I'm trying to setup mouse hook on background thread.
delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
LowLevelMouseProc _proc = HookCallback;
SetWindowsHookEx(PInvoke.WH_MOUSE_LL, _proc, IntPtr.Zero, 0);
and
IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam){/**/}
If I put this on the main window thread everything works until window has to do more complicated work which causes mouse to stop responding for the duration of that work (updating multiple children in panel for example).
If I start a new Thread and from there set up the hook, the problem is the thread simply exits after setting up the hook and callback function is never called.
Is there a way to keep thread alive for this purpose? Or if another way exists for hooking up mouse without risking unresponsive behavior?
I accidentaly noticed that when worker thread executes
GetMessage(out msg, new IntPtr(0), 0, 0);
No message is ever recieved but thread is being kept alive for the required purpose. Also I need an elegant way to close the thread, but GetMessage never returns.
I don't quite understand all these messages, I simply want to be able to hook up mouse and secure it against freezing.
Any help is appriciated.