/// <returns>False iff the command timed out</returns> /// <exception cref="Exception">If the inner function threw an exception</exception> internal static bool TryExecuteWithTimeout <T>(Func <T> work, out T ret, out StackTrace timedOutTrace) { T innerResult = default(T); Exception innerEx = null; var thread = new Thread((() => { try { innerResult = work(); } catch (Exception e) { innerEx = e; } })) { Name = "Timeout thread", IsBackground = true }; thread.Start(); if (Debugger.IsAttached) { thread.Join(); } if (thread.Join(DefaultTimeout)) { if (innerEx != null) { ExceptionUtility.RethrowWithNoStackTraceLoss(innerEx); } ret = innerResult; timedOutTrace = null; return(true); } else { #pragma warning disable 612,618 thread.Suspend(); timedOutTrace = new StackTrace(thread, false); thread.Resume(); #pragma warning restore 612,618 thread.Abort(); ret = default(T); return(false); } }
private MethodResult Execute(ITestCommand inner) { if (!_tasks.Any()) { foreach (var testCommand in _innerCommands) { var command = (DelegatingTestCommand)testCommand; Func <MethodResult> action = () => { var innerCommand = (IMethodInfo)_methodInfoField.GetValue(command.InnerCommand); return(new LifetimeCommand(command, innerCommand).Execute(null)); }; var task = new Task <MethodResult>(action, TaskCreationOptions.LongRunning); _tasks.Add(testCommand, task); } StartAllTasks(_tasks.Values); } try { return(_tasks[inner].Result); } catch (AggregateException e) { if (e.InnerExceptions.Count == 1) { //This looks better in nunit ExceptionUtility.RethrowWithNoStackTraceLoss(e.InnerException); throw; } else { throw; } } }