0

Hi made a hook library that catches global events from the keyboard and mouse.

   `private bool Install()     //this function install all the hooks (mouse/keyobard) 
    {                            
        bool started=true;
        started &= _globalMouse.Install();
        started &= _globalKeyboard.Install();

        if(!started)
        {
            Uninstall();
        }
        return started;
    }

    private void Uninstall() //this function unistall all the hooks
    {
        _globalMouse.Uninstall();
        _globalKeyboard.Uninstall();
    }


    private void run()   // this is the method that is called by the thread to register and 
                         // there should be caught all the global events (mouse/keyboard) 
    {

        Instance.Install();
       while(_running)
       {
           Thread.Sleep(0);
       }

        Instance.Uninstall();
    }`

this is the creation and starting thread function:

        public bool Start()
        {
            if(_thread!=null && _thread.IsAlive)
            {
                return true;
            }
            _running = true;
            _thread = new Thread(run);
            _thread.Start();
            return _running;
        }

The problem is that if I use the method Install()directly from my form it's working. If it was the method start() that should start the thread it isn't working (yes the thread remains alive, and yes I caught the needed event in the form correctly (worked with Install() function).

I read on different pages that I need a cycle to pump messages from/to the system, but I don't know how to make it.

My question is how I can make my thread able to catch global events that worked fine in my form thread.

Kristof U.
  • 1,263
  • 10
  • 17
4bottiglie
  • 523
  • 7
  • 22
  • 2
    You have to pump a message loop, Application.Run(). Altogether a better alternative than a while() loop that burns 100% core :) – Hans Passant Sep 05 '14 at 16:42
  • it's normaly that my cursor keeps flickering ? and then how to stop the thread? i tried that before and i though that was merging the threads – 4bottiglie Sep 05 '14 at 16:43
  • Application.ExitThread() stops it. No idea what a flickering cursor could mean, you give no context at all. If you already have a form then there's no point in doing this at all. – Hans Passant Sep 05 '14 at 16:54
  • if i call Application.ExitThread(); from my form, the form stops but my thread is still going, how to call that function from the second thread context? – 4bottiglie Sep 05 '14 at 17:09
  • http://stackoverflow.com/a/21684059/17034 – Hans Passant Sep 05 '14 at 17:51
  • thanks a lot, i lost a day for those things – 4bottiglie Sep 05 '14 at 18:11
  • Hmm, threading ought to cost you at least a month of your life. You haven't actually got it working yet, those events fire on the wrong thread :) – Hans Passant Sep 05 '14 at 18:14
  • What do you mean? That was working for me. Will it make me latter problems? – 4bottiglie Sep 05 '14 at 22:29

0 Answers0