本文介绍了ContinueWith在主线程任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,如果这是一个简单的问题;我不能句话在一个通用的足够的方法来搜索答案



考虑以下代码:

  VAR任务= Task.Factory.StartNew(()=>无论()); 
task.ContinueWith(回调,TaskScheduler.FromCurrentSynchronizationContext())



究竟它是如何确定的当回调方法将执行?



它会等待,直到主线程当前做完不管它是干什么的,还是会被调用后立即异步调用完成?并将此回调主线程返回到不管它在做之前,之前完全执行呢?


解决方案

When the asynchronous call completes, the continuation will be scheduled. The effect of that scheduling depends on the scheduler (of course) but for a "normal" WPF or Windows Forms message loop, I'd expect it to be scheduled in a similar way to a call to Control.BeginInvoke or Dispatcher.BeginInvoke - in other words, when the "main" thread has finished the rest of the tasks which had been scheduled before this one.

I wouldn't expect the main thread to magically stop what it's doing and execute the continuation. It could make the continuation a high-priority task in its queue, however.

这篇关于ContinueWith在主线程任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:57
查看更多