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

  • Home
  • SEARCH
  • 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 6477197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:55:08+00:00 2026-05-25T06:55:08+00:00

I written a some program. It hook a user input through SetWindowsHookEx. and it

  • 0

I written a some program. It hook a user input through SetWindowsHookEx. and it works very well.

and then I want to know how implement WindowsHook in different thread, for learning C#.

but i’m newly in C#, so i can’t find answer.

help me.

thanks.

edit

here is my InputHook class. some codes were abbreviated.

public class InputHooker
{

    public delegate IntPtr inputHookedDelegate(int nCode, IntPtr wParam, IntPtr lParam);
    public inputHookedDelegate keyHookHandler;


    public InputHooker()
    {
        keyHookHandler = onKeyHooked;
    }

    public void StartInputHook()
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            curKeyHookID = SetWindowsHookEx(WH_KEYBOARD_LL, keyHookHandler, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private IntPtr onKeyHooked(int nCode, IntPtr wParam, IntPtr lParam)
    {
        return CallNextHookEx(curKeyHookID, nCode, wParam, lParam);
    }

}

And I tried like following code;

        MyInputHooker = new InputHooker();
        Thread myInputHookThread = new Thread(new ThreadStart(MyInputHooker.StartInputHook));
        myInputHookThread.Name = "UOCInputHookThread";
        myInputHookThread.Priority = ThreadPriority.Normal;
        myInputHookThread.Start();

but it dosen’t works. (SetWindowsHookEx was successed, but onKeyHooked dosen’t called in keyboard pressed.) instead, follwing code works very well.

        MyInputHooker = new InputHooker();
        MyInputHooker.StartInputHook();
  • 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-05-25T06:55:09+00:00Added an answer on May 25, 2026 at 6:55 am

    The reason this is failing can be found in the WinAPI documentation here:

    http://msdn.microsoft.com/en-us/library/ms644985(v=vs.85).aspx

    This hook is called in the context of the thread that installed it.
    The call is made by sending a message to the thread that installed the
    hook. Therefore, the thread that installed the hook must have a
    message loop.

    What’s happening is that your newly-created thread is setting the hook and exiting immediately, so when Windows wants to call your hook procedure by sending a message to the thread, it can’t find it because it has already exited.

    If this is just for learning purposes, you can put a message pump in the thread by calling Application.Run at the end of your StartInputHook method. (At least I think that will work — haven’t tried it myself.) For real code, the best thing to do is call SetWindowsHookEx in your main application thread. I’m assuming that you’re doing that already in the case where it works and the application’s message pump is taking care of this for you.

    Honestly, if this is just for learning C#, SetWindowsHookEx might be a bit too advanced. Using it requires knowledge of how a lot of things work under the covers.

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

Sidebar

Related Questions

I have written a program in c, that do some calculations then creates a
I have written some sample program and DLL to learn the concept of DLL
I've written some code which is a mini simple imitation of messenger program. In
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a data acquisition program written in C++ (Visual Studio 6.0). Some clients
I have written some C# which connects to a live production database. I want
How well optimized is Java's parallel collecting GC for multithreaded environments? I've written some
When I see a small program which is written for some students, I often
I have written a wizard in C++ which installs some files to the program
I have written a program to process some data written to disk in big-endian

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.