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(); } }
public void Stop() { try { lock (this) { // Set the flag, telling the thread it's gotta wrap up. KeepWorking = false; // In case it 's waiting, signal it to stop. Monitor.Pulse(this); } // Join it for 20 millis. MonitorThread.Join(20); // if it's not done by now, abort it. MonitorThread.Abort(); // fail the current task. if (Queue.CurrentTask != null) { Queue.Fail("Service is stopping."); } // Attempt to release the application domain, if it 's there. releaseAppDomain(); Log.Info("Stopped monitor thread."); Log.DebugFormat("Thread Identifier: {0}", MonitorThread.Name); MonitorThread = null; } catch (Exception exception) { Log.Error("Failed to stop monitor thread.", exception); } }