Hello everyone!
I'm working with Windows 7 and my application is having some troubles with the SetWindowsHookEx API - everything works fine but when my application is not responsive (gets into a loop) the hook automatically stops (LowLevelHookTimeOut) thats because my application haven't processed the hook messages in time (In this case, mouse movements).
When I install the hook a method called "MouseProc" gets the hook messages (mouse movements and clicks) the problem is that this method ("MouseProc") is on the same thread of my app UI - is there is any way to make the "MouseProc" method be on a separate thread? I have attached the class.
When the user declares this class as new the hook is automaticlly installed, here is an example:
Code:
Dim WithEvents MHook As MouseHook
Private Sub Form_Load(Some Args)
MHook = New MouseHook()
'Mouse hook installed
End Sub
I thought calling the API in another thread when the class being declared as new:
Code:
Public Sub New()
Dim t as New Thread(AddressOf InstallHook)
t.Start()
End Sub
Private Sub InstallHook()
''Installs a Low Level Mouse Hook
MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub
But it's not really working.
Thank you and hope a solution will be found... I tried different stuff but nothing really works yet....
Because I need the UI thread to control other objects on the form (buttons, etc) which creates an error if I will handle them in another thread. I only need that the MouseProc method will be called in another thread.
Just because you need to update the UI during a task doesn't mean that the entire task has to be done on the UI thread. If you're getting errors it's because you're not delegating back to the UI thread when you should be. Follow the CodeBank link in my signature and check out my post on Accessing Controls From Worker Threads.
Just because you need to update the UI during a task doesn't mean that the entire task has to be done on the UI thread. If you're getting errors it's because you're not delegating back to the UI thread when you should be. Follow the CodeBank link in my signature and check out my post on Accessing Controls From Worker Threads.
Hello, thank you for your suggestion but I need to the "MouseProc" method to be called from another thread because of the UI being unresponsive (and some other reasons I mentioned) in certain situations, and when my UI isn't responsive the hook calls are not being processed in time which means that the hook in Windows 7 will stop because of the LowHookTimeOut, I know I can use My.Application.DoEvents() to make the UI responsive but I don't want to use this method.
I decided to uninstall the hook every time my form is not responsive and then reinstall it (which seems to solve my problem).
Last question, is there is any way to detect (using the class I attached above) when the hook was stopped? (When the hook had stopped sending messages to my application)
Last edited by shynet; Mar 28th, 2010 at 04:49 AM.