Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:31:06+00:00 2026-06-07T03:31:06+00:00

In my C# app I want to do the keyhook but so, that the

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:31:07+00:00Added an answer on June 7, 2026 at 3:31 am

    The point of the message loop is to give Windows an opportunity to call your hook procedure. There is no other way for it to call your code. It cannot break in and force your thread to run code, that would cause horrible re-entrancy problems. The hook procedure can only run when your code calls GetMessage(). And will run in the context of that thread. You can verify this by looking at the return value of GetCurrentThreadId().

    Using a thread is otherwise no fix for a slow hook procedure. Windows won’t dispatch any other input events until the hook procedure returns. Make it slow enough and Windows will kill the hook forcibly to recover. Consider using a worker thread that you feed from a thread-safe queue instead, allowing your hook procedure to quickly return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this app I want that as I click on button the data from
For a web app I want to write/obtain an embeddable widget that will show
I want to use push notification in my app.i want to know that do
I have an app and want to put the main thread to sleep for
In ipad app I want to set UISplitViewController method set in portrait mode.(i.e Like
I want do an app,i want it look the same as this one: I
In my app I want create thousands files in background, but when I execute
In my app I want to catch reactions from my UITableView but I do
In my app i want to diaplay the notifications. A background service will get
the app I want to create will have an interface very like Keynote, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.