gpt4 book ai didi

c# - Fire and Forget 没有得到安排

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:05 26 4
gpt4 key购买 nike

我正在使用 Task.Run(() => this.someMethod()) 来安排后台作业。我对操作结果不感兴趣,需要继续申请流程。

但是,有时我的后台任务很长时间都没有安排。自从我们从 .Net 4.7 迁移到 4.5 后,这种情况就开始发生了。即使在调试时,断点也没有命中或在相当长的延迟(> 10 分钟)后命中。

有没有人注意到这种行为或知道是什么原因造成的?

我在 i7 内核、16 GB RAM 上运行。

最佳答案

让您的任务甚至需要 10 分钟才能开始听起来很可疑。我猜你的系统负载很重,或者你有很多任务在运行。

我要攻击后者(针对特定情况)。

TaskCreationOptions

LongRunning Specifies that a task will be a long-running, coarse-grained operation involving fewer, larger components than fine-grained systems. It provides a hint to the TaskScheduler that oversubscription may be warranted. Oversubscription lets you create more threads than the available number of hardware threads. It also provides a hint to the task scheduler that an additional thread might be required for the task so that it does not block the forward progress of other threads or work items on the local thread-pool queue.

var task = new Task(() => MyLongRunningMethod(),TaskCreationOptions.LongRunning);
task.Start();

这是来自 Stephen Toub - MSFT 的引述关于这个post

Under the covers, it's going to result in a higher number of threads being used, because its purpose is to allow the ThreadPool to continue to process work items even though one task is running for an extended period of time; if that task were running in a thread from the pool, that thread wouldn't be able to service other tasks. You'd typically only use LongRunning if you found through performance testing that not using it was causing long delays in the processing of other work.

如果不查看您所有的代码,很难知道您的问题是什么,但是我将其发布为建议。

关于c# - Fire and Forget 没有得到安排,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50500338/

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