public void Start() { lock (_locker) { if (!_isRunning) { _prevTimeApplication = DateTime.Now; _activeWindows = new Dictionary <IntPtr, WindowInfoObject>(); _taskCancellationTokenSource = new CancellationTokenSource(); _applicationsQueue = new QueueHookConcurrentAsync <object>(_taskCancellationTokenSource.Token); Task.Factory.StartNew(() => { _eventHook = new WindowEventHook(_shf); _eventHook.WindowCreated += WindowCreated; _eventHook.WindowDestroyed += WindowDestroyed; _eventHook.WindowActivated += WindowActivated; }, CancellationToken.None, TaskCreationOptions.None, _shf.GetTaskScheduler()).Wait(); _lastEventWasLaunched = false; _lastHwndLaunched = IntPtr.Zero; Task.Factory.StartNew(ApplicationConsumer); _isRunning = true; } } }
public static void Main() { // hook logic using var hook = new WindowEventHook(WindowEvent.EVENT_MIN, WindowEvent.EVENT_MAX); hook.EventReceived += (s, e) => Console.WriteLine(Enum.GetName(typeof(WindowEvent), e.EventType)); hook.HookGlobal(); // simple message loop while (true) { var res = GetMessage(out var msg, IntPtr.Zero, 0, 0); if (res == 0) { break; } TranslateMessage(ref msg); DispatchMessage(ref msg); } }