void Watch(object sender, EventArgs e) { List <Process> updatedProcesses = Process.GetProcesses().ToList(); //Create a variable for the updated processes list. if (updatedProcesses != processes) //If the process list is different from the updated version... { if (updatedProcesses.Count < processes.Count) //If the updated list is smaller, which means that a process was killed... { Debug.WriteLine("processes.Count: " + processes.Count); Debug.WriteLine("updatedProcesses.Count: " + updatedProcesses.Count); ProcessKilled?.Invoke(this, new ProcessEventArgs(GetKilledProcesses(updatedProcesses))); //Invoke the ProcessKilled event with the process that was killed. } if (updatedProcesses.Count > processes.Count) //If the updated list is larger, which means that a new process was started... { Debug.WriteLine("processes.Count: " + processes.Count); Debug.WriteLine("updatedProcesses.Count: " + updatedProcesses.Count); ProcessStarted?.Invoke(this, new ProcessEventArgs(GetStartedProcesses(updatedProcesses))); //Invoke the ProcessStarted event with the process that was started. } } processes = new List <Process>(updatedProcesses); //Replace the process list with the updated version. }
protected void OnProcessKilled(Process process) { ProcessKilled?.Invoke(this, process.ToEventArgs()); }