本文经过PushFrame,实现异步转同步html
首先有一个异步方法,以下异步任务延时2秒后,返回一个结果异步
1 private static async Task<string> TestWithResultAsync() 2 { 3 Debug.WriteLine("1. 异步任务start……"); 4 await Task.Delay(2000); 5 Debug.WriteLine("2. 异步任务end……"); 6 return "2秒之后"; 7 }
在UI线程执行此任务,尝试转化为同步async
1 private void PushFrameTaskResult_OnClick(object sender, RoutedEventArgs e) 2 { 3 var result = AwaitByPushFrame(TestWithResultAsync()); 4 Debug.WriteLine($"PushFrameTaskResult_OnClick end:{result}"); 5 }
PushFrame异步转同步的实现:post
1 public static TResult AwaitByPushFrame<TResult>(Task<TResult> task) 2 { 3 var frame = new DispatcherFrame(); 4 task.ContinueWith(t => 5 { 6 frame.Continue = false; 7 }); 8 Dispatcher.PushFrame(frame); 9 return task.Result; 10 }
测试结果:测试
Task不带返回值的处理:spa
1 public static void AwaitByPushFrame(Task task) 2 { 3 var frame = new DispatcherFrame(); 4 task.ContinueWith(t => 5 { 6 frame.Continue = false; 7 }); 8 Dispatcher.PushFrame(frame); 9 }
PS:pushFrame虽然可以实现异步转同步,但也有缺陷,能够选择性的使用线程
PushFrame的详细原理及缺陷,可参考小伙伴水先生的《 深刻了解 WPF Dispatcher 的工做原理(PushFrame 部分)》code