public RVList <LNode> ProcessFileWithThreadAbort(InputOutput io, Action <InputOutput> onProcessed, TimeSpan timeout) { if (timeout == TimeSpan.Zero || timeout == TimeSpan.MaxValue) { return(ProcessFile(io, onProcessed)); } else { Exception ex = null; var thread = new ThreadEx(() => { try { ProcessFile(io, null); } catch (Exception e) { ex = e; ex.PreserveStackTrace(); } }); thread.Start(); if (thread.Join(timeout)) { onProcessed(io); } else { io.Output = new RVList <LNode>(F.Id("processing_thread_timed_out")); thread.Abort(); thread.Join(timeout); } if (ex != null) { throw ex; } return(io.Output); } }
public void BasicChecks() { ThreadLocalVariable <int> threadVar = new ThreadLocalVariable <int>(123); Thread parent = Thread.CurrentThread; bool eventOccurred = false; bool valueOk = true, eventOk = true; bool stop = false; bool started = false; ThreadEx t = new ThreadEx(delegate(object o) { started = true; try { if ((int)o != 123 || threadVar.Value != 123) { valueOk = false; } } catch { valueOk = false; } while (!stop) { GC.KeepAlive(""); // Waste time } started = false; }); EventHandler <ThreadStartEventArgs> eh = null; ThreadEx.ThreadStarting += (eh = delegate(object o, ThreadStartEventArgs e) { eventOccurred = true; if (e.ChildThread != t || e.ParentThread != parent) { eventOk = false; } ThreadEx.ThreadStarting -= eh; }); Assert.IsFalse(t.IsAlive); Assert.AreEqual(System.Threading.ThreadState.Unstarted, t.ThreadState); t.Start(123); Assert.IsTrue(t.IsAlive); Assert.IsTrue(eventOccurred); Assert.IsTrue(eventOk); while (!started) { ThreadEx.Sleep(0); } Assert.AreEqual(System.Threading.ThreadState.Running, t.ThreadState); stop = true; Assert.IsTrue(t.Join(5000)); Assert.IsTrue(valueOk); Assert.IsFalse(started); }
/// <summary> /// Stops the worker thread which monitors for changes of status /// of the adapter. This must be done, if monitoring has been /// started, before the object is destroyed. /// </summary> public void StopStatusMonitoring() { if (Active) { if (mq != null) { // Close the point-to-point message queue. This should // cause any waits on it to return, allowing the thread // to exit. PointToPointMsgQueue q = mq; mq = null; q.Close(); // Wait for the monitoring thread to exit. mqt.Join(5000); mqt = null; } } }