c# - 在哪个调度程序 Task.ContinueWith() 上运行?

标签 c# task-parallel-library task

考虑以下代码:

// MyKickAssTaskScheduler is a TaskScheduler, IDisposable
using (var scheduler = new MyKickAssTaskScheduler())
{
    Task foo = new Task(() => DoSomething());
    foo.ContinueWith((_) => DoSomethingElse());
    foo.Start(scheduler);
    foo.Wait();
}

ContinueWith() 任务是否保证在我的调度程序上运行?如果不是,它将使用哪个调度程序?

最佳答案

StartNew、ContinueWith 将默认为 TaskScheduler.Current、Current 将返回默认调度程序,当未从任务 (MSDN) 中调用时。

为避免默认调度程序问题,您应该始终将显式 TaskScheduler 传递给 Task.ContinueWith 和 Task.Factory.StartNew。

ContinueWith is Dangerous

关于c# - 在哪个调度程序 Task.ContinueWith() 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31111951/

相关文章:

c# - 如何用递归方法实现并行?

c# - 异步 Task.WhenAll 超时

c# - 如何在不自动降级到同步模式的情况下调用异步方法?

c# - 异步更新数据库

c# - EF 如何使用 .include() 查询更多实体并使用存储库模式

c# - 一次启动多少个异步(非多线程)操作?这是可配置的吗?

c# - 任务并行库 (TPL) 是否处理竞争条件

c# - 内联任务何时发生?

c# - Visual Studio 中的 Azure Functions - 如何添加对另一个项目的引用

javascript - 将带有数据和文件的 javascript 对象发送到 asp.net core