,이 같은 내 testmethods을 작성할 수 있습니다
[TestMethod]
public void MyTest()
{
Helper.RunInWpfSyncContext(async() =>
{
Assert.IsNotNull(SynchronizationContext.Current);
await MyTestAsync();
DoSomethingOnTheSameThread();
});
}
내부 코드는 이제 WPF 동기화 컨텍스트에서 실행 및 MSTEST에 사용되는 모든 예외를 처리합니다. 도우미 방법은 Stephen Toub 출신이다 :
using System.Windows.Threading; // WPF Dispatcher from assembly 'WindowsBase'
public static void RunInWpfSyncContext(Func<Task> function)
{
if (function == null) throw new ArgumentNullException("function");
var prevCtx = SynchronizationContext.Current;
try
{
var syncCtx = new DispatcherSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(syncCtx);
var task = function();
if (task == null) throw new InvalidOperationException();
var frame = new DispatcherFrame();
var t2 = task.ContinueWith(x=>{frame.Continue = false;}, TaskScheduler.Default);
Dispatcher.PushFrame(frame); // execute all tasks until frame.Continue == false
task.GetAwaiter().GetResult(); // rethrow exception when task has failed
}
finally
{
SynchronizationContext.SetSynchronizationContext(prevCtx);
}
}