Results 1 to 7 of 7

Thread: SetWindowsHookEx and Threading

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    SetWindowsHookEx and Threading

    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....
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SetWindowsHookEx and Threading

    How about you do the OTHER thing on a different thread? That would be the logical option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Re: SetWindowsHookEx and Threading

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SetWindowsHookEx and Threading

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Re: SetWindowsHookEx and Threading

    Quote Originally Posted by jmcilhinney View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SetWindowsHookEx and Threading

    Then you're out of luck because it's not possible. I guess you'll just have to abandon this application, or do as I've already suggested.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    155

    Re: SetWindowsHookEx and Threading

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width