2

I have a global mouse hook in my app but after sometime (on some computers) the mouse hook procedure (MouseHookProc) suddenly stops getting called, I'm not sure why, I've searched for the reason and it seems like it has something to do with win7's LowLevelHooksTimeout

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);

private void AddMouseHook()
{
    MouseHookProcedure = new User32.HookProc(MouseHookProc);
    hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, (IntPtr)null, 0);
}

fix seems to be to add the mouse hook in a new thread but if I do that then the mouse hook doesn't gets called right from the beginning, and I can verify that SetWindowsHookEx does return value > 0 so hook got added properly, but again if it's added from another thread then "MouseHookProc" doesn't get invoked even once, if started from main thread then no problem, but just disappears after sometime (mostly after some heavy processing where my mouse will tend to freeze then after that "MouseHookProc" will not be called anymore

ThreadStart ts = new ThreadStart(AddMouseHook);
Thread th = new Thread(ts);
th.Start();
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.