public void Stop() { if (!running) { return; } DebugStub.WriteLine(String.Format("StoppableThread: Asking thread {0} to stop...", thread.ToString())); VTable.Assert(thread != null); StopThreadContract /*.Imp*/ terminatorImp = this.terminatorImpRef.Acquire(); terminatorImp.SendTerminate(); bool timeout_occurred = false; /* TODO * bool done; * do { * switch receive { * case terminatorImp.AckTerminate(): * done = true; * break; * * case terminatorImp.ChannelClosed(): * // good enough * done = true; * DebugStub.WriteLine(String.Format("Warning: Thread {0} closed its termination channel.", thread.ToString())); * DebugStub.WriteLine("The thread may have terminated unexpectedly (thrown an exception)."); * break; * * case timeout(TimeSpan.FromSeconds(10)): * DebugStub.WriteLine(String.Format("Warning: Thread {0} is ignoring request to terminate.", thread.ToString())); * done = false; * timeout_occurred = true; * break; * } * } while (!done); */ //delete terminatorImp; running = false; while (!doneEvent.WaitOne(TimeSpan.FromSeconds(10))) { DebugStub.WriteLine(String.Format("Warning: Thread {0} acknowledge stop request, but has not yet terminated.", thread.ToString())); timeout_occurred = true; } if (timeout_occurred) { DebugStub.WriteLine(String.Format("StoppableThread: Thread {0} successfully stopped.", thread.ToString())); } }
private void ThreadRoutine() { StopThreadContract /*.Exp*/ terminator_exp = terminatorExpRef.Acquire(); try { Run(terminator_exp); } finally { //delete terminator_exp; } }
protected StoppableThread() { StopThreadContract /*.Imp*/ terminator_imp; StopThreadContract /*.Exp*/ terminator_exp; StopThreadContract.NewChannel(out terminator_imp, out terminator_exp); terminatorImpRef = (terminator_imp); terminatorExpRef = (terminator_exp); running = false; thread = null; }
protected abstract void Run(StopThreadContract /*.Exp*/ terminator);