internal void RunTestWithTimeout(Func <TestExecutionContext, object> testFunc, TimeSpan timeout) { if (timeout <= TimeSpan.Zero) { _testReturnValue = testFunc(this); return; } Exception error = null; var cts = new CancellationTokenSource(); cts.CancelAfter(timeout); _cancellationToken = cts.Token; ParameterizedThreadStart thunk = syncObject => { try { _testReturnValue = testFunc(this); } catch (Exception ex) { error = ex; } lock (syncObject) { Monitor.Pulse(syncObject); } }; object monitorSync = new object(); bool timedOut; var thread = new Thread(thunk); lock (monitorSync) { thread.Start(monitorSync); timedOut = !Monitor.Wait(monitorSync, timeout); cts.Cancel(); } cts.Dispose(); _cancellationToken = CancellationToken.None; if (timedOut) { thread.Abort(); throw SpecFailure.TestTimedOut(timeout); } if (error != null) { throw error; } }