static void processStopEvent_EventArrived(object sender, EventArrivedEventArgs e) { //Stop code string processName = e.NewEvent.Properties["ProcessName"].Value.ToString(); if (processName.ToLower() == ProgramName.ToLower()) { using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry($"{ProgramName} closed.", EventLogEntryType.Information, 101, 1); } } }
static void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e) { try { string processName = e.NewEvent.Properties["ProcessName"].Value.ToString(); if (processName.ToLower() == ProgramName.ToLower()) { using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry($"{ProgramName} started. Repositioning.", EventLogEntryType.Information, 101, 1); } int processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value); Process process = Process.GetProcessById(processID); IntPtr ptr = process.MainWindowHandle; Rect posRect = new Rect(); GetWindowRect(ptr, ref posRect); Screen destinationMonitor; if (!string.IsNullOrEmpty(MonitorName)) { destinationMonitor = Screen.AllScreens.Where(f => f.DeviceName.ToLower() == MonitorName.ToLower()).First(); } else { destinationMonitor = Screen.AllScreens[MonitorNumber - 1]; } if (FullScreen) { MoveWindow(ptr, destinationMonitor.WorkingArea.X, destinationMonitor.WorkingArea.Y, destinationMonitor.WorkingArea.Width, destinationMonitor.WorkingArea.Height, true); } else { MoveWindow(ptr, destinationMonitor.WorkingArea.X, destinationMonitor.WorkingArea.Y, destinationMonitor.WorkingArea.Width, destinationMonitor.WorkingArea.Height, true); MoveWindow(ptr, destinationMonitor.WorkingArea.X, destinationMonitor.WorkingArea.Y, Width, Height, true); } using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry($"Finished repositioning {ProgramName} to monitor {MonitorNumber}.", EventLogEntryType.Information, 101, 1); } } } catch (Exception ex) { using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 101, 1); } throw; } finally { e.NewEvent.Dispose(); } }