public void MonitorProcess() { var app = new App { Name = "cmd", Program = "cmd.exe" }; var monitor = new MonitorThread(app); try { // Process won't start until this thread is idle var waitForProcess = new Thread(delegate() { for (int i = 0; i < 30; i++) { if (monitor.Process != null) { break; } else { Thread.Sleep((i + 1) * 100); } } }); waitForProcess.Start(); waitForProcess.Join(); Console.WriteLine("test"); Assert.IsNotNull(monitor.Process); Assert.IsFalse(monitor.Process.HasExited); var process1 = monitor.Process; monitor.Process.Kill(); waitForProcess = new Thread(delegate() { for (int i = 0; i < 30; i++) { if (monitor.Process != process1) { break; } else { Thread.Sleep((i + 1) * 100); } } }); waitForProcess.Start(); waitForProcess.Join(); Assert.IsNotNull(monitor.Process); monitor.Stop(); monitor.Join(); Assert.IsTrue(monitor.Process.HasExited); } finally { monitor.Stop(); monitor.Join(); } }
private void _Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // Stop and wait for task to end _MonitorThread.Stop(); _MonitorTask.Wait(); // Dispose of thread and task _MonitorThread.Dispose(); _MonitorTask.Dispose(); }