复制 保存
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Security.Permissions; using System.Reflection; namespace TestUnit { public class CrossThreadTestRunner { private Exception lastException; public void RunInMTA(ThreadStart userDelegate) { Run(userDelegate, ApartmentState.MTA); } public void RunInSTA(ThreadStart userDelegate) { Run(userDelegate, ApartmentState.STA); } private void Run(ThreadStart userDelegate, ApartmentState apartmentState) { lastException = null; Thread thread = new Thread( delegate() { try { userDelegate.Invoke(); } catch (Exception e) { lastException = e; } }); thread.SetApartmentState(apartmentState); thread.Start(); thread.Join(); if (ExceptionWasThrown()) ThrowExceptionPreservingStack(lastException); } private bool ExceptionWasThrown() { return lastException != null; } [ReflectionPermission(SecurityAction.Demand)] private static void ThrowExceptionPreservingStack(Exception exception) { FieldInfo remoteStackTraceString = typeof(Exception).GetField( "_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic); remoteStackTraceString.SetValue(exception, exception.StackTrace + Environment.NewLine); throw exception;
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/
领测软件测试网最新更新