/// <inheritdoc/> public void Run(Action action, PlatformApartmentState apartmentState, bool waitForCompletion) { if (action == null) { return; } Exception exThrown = null; var thread = new System.Threading.Thread(() => { try { action(); } catch (Exception e) { exThrown = e; } }); ApartmentState state = Enum.TryParse(apartmentState.ToString(), out state) ? state : ApartmentState.MTA; thread.SetApartmentState(state); thread.IsBackground = true; thread.Start(); if (waitForCompletion) { thread.Join(); if (exThrown != null) { throw exThrown; } } }
private void SetupForExecutionThreadApartmentStateTests(PlatformApartmentState apartmentState) { this.mockThread = new Mock <IThread>(); this.runTestsInstance = new TestableBaseRunTests( $@"<RunSettings> <RunConfiguration> <ExecutionThreadApartmentState>{apartmentState}</ExecutionThreadApartmentState> </RunConfiguration> </RunSettings>", testExecutionContext, null, this.mockTestRunEventsHandler.Object, this.mockTestPlatformEventSource.Object, null, this.mockThread.Object, this.mockRequestData.Object); TestPluginCacheTests.SetupMockExtensions(new string[] { typeof(BaseRunTestsTests).GetTypeInfo().Assembly.Location }, () => { }); var assemblyLocation = typeof(BaseRunTestsTests).GetTypeInfo().Assembly.Location; var executorUriExtensionMap = new List <Tuple <Uri, string> > { new Tuple <Uri, string>(new Uri(BaseRunTestsExecutorUri), assemblyLocation) }; this.runTestsInstance.GetExecutorUriExtensionMapCallback = (fh, rc) => { return(executorUriExtensionMap); }; }
/// <inheritdoc/> public void Run(Action action, PlatformApartmentState apartmentState, bool waitForCompletion) { if (apartmentState == PlatformApartmentState.STA) { throw new ThreadApartmentStateNotSupportedException(); } if (action == null) { return; } Task task = Task.Run(action); if (waitForCompletion) { task.Wait(); } }
/// <inheritdoc/> public void Run(Action action, PlatformApartmentState apartmentState, bool waitForCompletion) { if (apartmentState == PlatformApartmentState.STA) { throw new ThreadApartmentStateNotSupportedException(); } if (action == null) { return; } Exception exThrown = null; var thread = new Thread(() => { try { action(); } catch (Exception e) { exThrown = e; } }); // ApartmentState is not supported in netcoreapp1.0. thread.IsBackground = true; thread.Start(); if (waitForCompletion) { thread.Join(); if (exThrown != null) { // Preserve the stacktrace when re-throwing the exception. ExceptionDispatchInfo.Capture(exThrown).Throw(); } } }
/// <inheritdoc/> public void Run(Action action, PlatformApartmentState apartmentState, bool waitForCompletion) { if (apartmentState == PlatformApartmentState.STA) { throw new ThreadApartmentStateNotSupportedException(); } if (action == null) { return; } Exception exThrown = null; var thread = new Thread(() => { try { action(); } catch (Exception e) { exThrown = e; } }); // ApartmentState is not supported in netcoreapp1.0. thread.IsBackground = true; thread.Start(); if (waitForCompletion) { thread.Join(); if (exThrown != null) { throw exThrown; } } }
/// <inheritdoc /> public void Run(Action action, PlatformApartmentState platformApartmentState, bool waitForCompletion) { throw new NotImplementedException(); }