private static void EventCallback( IntPtr hWinEventHook, uint iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime) { ProcessStateChangedEventArgs processStatedChangeEventArgs = new ProcessStateChangedEventArgs(); processStatedChangeEventArgs.ProcessProxy.MainWindowHandle = hWnd.ToInt32(); processStatedChangeEventArgs.EventType = (int)iEvent; if (processStatedChangeEventArgs.EventType == (int)EVENT_SYSTEM_FOREGROUND) { processStatedChangeEventArgs.StateChangeType = ProcessStateChangedEventArgs.StateChangeTypes.BroughtForeground; } _eventBasedProcessMonitor?.ProcessStateChanged?.Invoke(_eventBasedProcessMonitor, processStatedChangeEventArgs); }
private void OnEventArrived(object sender, EventArrivedEventArgs e) { string eventType = e.NewEvent.ClassPath.ClassName; Win32Process process = new Win32Process(e.NewEvent["TargetInstance"] as ManagementBaseObject); if (ProcessStateChanged != null) { ProcessStateChangedEventArgs processStateChangedEventArgs = new ProcessStateChangedEventArgs(); processStateChangedEventArgs.ProcessProxy.Id = (int)process.ProcessId; processStateChangedEventArgs.ProcessProxy.ProcessName = process.Name; switch (eventType) { case "__InstanceCreationEvent": processStateChangedEventArgs.StateChangeType = ProcessStateChangedEventArgs.StateChangeTypes.Created; break; case "__InstanceModificationEvent": processStateChangedEventArgs.StateChangeType = ProcessStateChangedEventArgs.StateChangeTypes.Modified; break; case "__InstanceDeletionEvent": processStateChangedEventArgs.StateChangeType = ProcessStateChangedEventArgs.StateChangeTypes.Deleted; break; } if (processStateChangedEventArgs.StateChangeType != ProcessStateChangedEventArgs.StateChangeTypes.Modified) { ProcessStateChanged(this, processStateChangedEventArgs); } } }