gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:17:45 24 4
gpt4 key购买 nike

考虑以下代码:

// 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com