Issues using Globalhook/System Freeze

Feb 28, 2014 at 6:02 AM
Hello,

I'm somewhat new to C# on the whole and I'm developing a small program to render my backlog of CAD stuff when I'm idle. I'm using this library but having major problems such as input frozen upon the launch of my program and from there out no user input is detected by the hooks. I am using Windows 8.1 x64 and compiling both the DLL and my own executable towards .NET 4.0

This is my code (just a small test of the hooks):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Windows.Forms;

namespace HookTest1
{
    class Program
    {
        static MouseHookListener mouseListener;
        static KeyboardHookListener keyListener;

        static void Main(string[] args)
        {
            Activate();

            while (true)
            {
                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            }
        }

        private static void OnUserActivity()
        {
            Console.Write("UserAct");
        }

        private static void Activate()
        {
            mouseListener = new MouseHookListener(new GlobalHooker());
            mouseListener.Enabled = true;
            mouseListener.MouseDown += OnMouseActivity;
            mouseListener.MouseMove += OnMouseActivity;
            mouseListener.MouseWheel += OnMouseActivity;

            keyListener = new KeyboardHookListener(new GlobalHooker());
            mouseListener.Enabled = true;
            keyListener.KeyDown += OnKeyActivity;
        }

        private static void OnMouseActivity(Object sender, System.Windows.Forms.MouseEventArgs e)
        {
            OnUserActivity();
        }

        private static void OnKeyActivity(Object sender, System.Windows.Forms.KeyEventArgs e)
        {
            OnUserActivity();
        }
    }
}
Sorry again for the double post, I'm new to this. Thanks in advance!
Mar 26, 2014 at 5:24 AM
I cannot help about your COM exception problem, but in this case:
when you substitute the "while (true)" block with a simple "System.Threading.Thread.Sleep(5000);", you will observe that the program will freeze for exact 5 seconds.
The problem here is that, the Thread.Sleep() Method will pause the whole process, together with the mouseListener EventTrigger. Since the Hook works like an intermediate layer between system hardware input and operation system (it record and then pass it to OS), all later inputs are either cached or discarded, causing a "freeze", until the main thread resumed.
In this case, I recommend an alternative walkaround: using a C# Timer might (or might not) be better, depending on your requirement. Or you can always rely on the Windows Task Scheduler (it checks if system is idle every 15 mins).
Anyway, thanks for the reporting. This exposed a serious defect of the library. I'll keep an eye on the code and test if I can detach the hook process from the main thread without significant performance impact.