In my C# app I want to do the keyhook but so, that the method invoked when the key is pressed is invoked in another thread. In this way it will not happen, that a key is omitted by my program, because my application was busy with something else. How can I make it in another thread? I have found information that, depending on the thread in which I create the hook, the method invoked when the key is pressed will be invoked in that thread, but that does not happen. Regardless of the thread in which I hooked it, this method is always invoked in the main thread. Please help. This is the outline of my code:
int HookProc(int code, int wParam, ref lParamStruct lParam)
{
// ... this should be invoked in another thread - thread th, which I use to call SetWindowsHook
}
void SetWindowsHook()
{
lpMsg msg = new lpMsg();
PeekMessage(out msg, new IntPtr(0), 0, 0, PM_NOREMOVE);
hhook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, new IntPtr(0), 0);
while (GetMessage(out msg, new IntPtr(0), 0, 0))
{
DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
if (abortThreadFlag)
break;
}
UnhookWindowsHookEx(hhook);
}
SetWindowsHook is invoked in this way, but id doesn't force the method to be invoked in another thread:
Thread th = new Thread(SetWindowsHook);
th.Start()