收录日期:2020/06/02 19:15:40 时间:2009-08-07 16:29:26 标签:c#,windows-mobile,compact-framework,console

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.

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();

If it going to run on devices that are owned by your company then why not run a small windows program in background. I mean just hide the window. Let it sit in your task bar. Click on this link for more information on use of notification icon in CF.

not sure this will help but with native code youd call

LRESULT CallNextHookEx(
    HHOOK hhk,
    int nCode,
    WPARAM wParam,
    LPARAM lParam
);

in your handler to execute the default handling behaviour, havent tested this but i think if you dont call the next handler in the chain, nothing will happen

more info: http://msdn.microsoft.com/en-us/library/ms644974%28VS.85%29.aspx . the link contains some managed code samples which may help

hth