C# – Keep a Windows Mobile Console App Running

ccompact-frameworkconsolewindows-mobile

I have a windows mobile app that look like this:

class Program
{
    static void Main(string[] args)
    {
        RunHook runHook = new RunHook();

    }
}

class RunHook
{
    private HookKeys hook;
    public RunHook()
    {
        hook = new HookKeys();
        hook.HookEvent += EventForHook;
    }

    private void EventForHook(HookEventArgs e, KeyBoardInfo keyBoardInfo, 
      ref Boolean handled)
    {
        if ((keyBoardInfo.scanCode == 4) && (keyBoardInfo.vkCode == 114))
            handled = true;
    }
}

It will create a hook into the keyboard (I know that is frowned on by some). My issue is that I need the Main method to never return. This is going to run on devices owned by my company and we are using this to disable the phone hardware keys.

This seems like it should be simple, but I am stuck on it.

On normal .NET I would just call Console.Readline(), but that does not work on Windows Mobile Compact Framework. I have also tried Thread.Sleep(0), but it does not work either.

Thanks for any feedback.

Best Answer

Thread.Sleep(0) sleeps for zero milliseconds.

You probably want Thread.Sleep(Timeout.Infinite).

You might also consider creating an EventWaitHandle:

class Program
{
    static public ManualResetEvent StopMain;

    static void Main(string[] args)
    {
        StopMain = new ManualResetEvent(false);
        RunHook runHook = new RunHook();
        StopMain.WaitOne();  // waits until signalled
    }
}

Then, if you were ever ready to exit Main(), you could call (from another thread):

Program.StopMain.Set();