public void TestAbortThread() { Lock sync = new Lock(); TimedCondition aborted = new TimedCondition(sync); Thread testThread = new Thread(delegate() { try { Log("Test thread sleeping for 8 seconds.."); Thread.Sleep(8000); } catch (ThreadAbortException) { Log("Test thread aborted.."); aborted.Signal(); } Fail("This line should never be executed"); }); testThread.Start(); sync.Acquire(); Log("Aborting test thread in 2 seconds. Sleeping..."); Thread.Sleep(2000); Log("...awake! Going to abort the test thread."); testThread.Abort(); Log("Called testThread.Abort()"); try { aborted.Await(12000); } catch (ConditionTimedOutException) { Fail("Thread was not aborted as expected!"); } }
public void TestAbortThreadWithState() { Lock sync = new Lock(); TimedCondition aborted = new TimedCondition(sync); Thread testThread = new Thread(delegate() { try { Log("Test thread sleeping for 8 seconds.."); Thread.Sleep(8000); } catch (ThreadAbortException e) { if (e.ExceptionState != null && e.ExceptionState.ToString() == "Exception state") { Log("Test thread aborted with state: " + e.ExceptionState); aborted.Signal(); } else { Log("Test thread aborted with unknown state: " + e.ExceptionState); } } Fail("This line should never be executed"); }); testThread.Start(); sync.Acquire(); Log("Aborting test thread in 2 seconds"); Thread.Sleep(2000); testThread.Abort("Exception state"); try { aborted.Await(12000); } catch (ConditionTimedOutException) { Fail("Thread was not aborted as expected!"); } }