TaskContinuations 与异步 lambdas 同步运行 [英] TaskContinuations ran synchronously with async lambdas
问题描述
如果我们有 aaa 任务,并且为该任务创建一个延续(通过 ContinueWith
),使用 TaskContinuationOptions.RunSynchronously
,如果方法在 中执行ContinueWith
如下:
If we have a a a task, and for that task we create a continuation (via ContinueWith
), with TaskContinuationOptions.RunSynchronously
, if the method being executed in ContinueWith
is the following:
static async Task SimpleMethodContinuationAsync(Task antecedentTask, object state)
{
// some very lightweight, thread-safe synchronous code.
await Task.Delay(5000); // Simulate async work.
// continuation of some more very lightweight, thread-safe synchronous code.
}
await
之后的部分 - 它会有效地释放开始执行任务延续的线程吗?本续篇的其余部分将在哪个线程上继续?
The part after the await
- will it effectively release the thread that started executing the task continuation? And on which thread will the rest of this continuation resume?
问题是在没有 SynchronizationContext
存在于上述延续的先行任务时提出的,但我听说 SynchronizationContext
不流经 ContinueWith
方法调用?是真的吗?
The question is asked when there is no SynchronizationContext
present for the antecedent task of the continuation above, but I've heard that SynchronizationContext
does not flow through ContinueWith
method calls? Is that true?
推荐答案
我第二个 Marc 的评论:你应该避免ContinueWith
;这是一个低级 API,具有潜在危险的默认行为.
I second Marc's comment: you should avoid ContinueWith
; it's a low-level API that has potentially dangerous default behavior.
await 之后的部分 - 它会有效释放开始执行任务延续的线程吗?
The part after the await - will it effectively release the thread that started executing the task continuation?
await
会做到这一点,是的.
The await
will do that, yes.
本续集的其余部分将在哪个线程上继续?... 当上述延续的先行任务没有 SynchronizationContext 时会问这个问题
And on which thread will the rest of this continuation resume? ... The question is asked when there is no SynchronizationContext present for the antecedent task of the continuation above
很可能是线程池线程完成了Task.Delay
返回的Task
.这是因为 await
使用 TaskContinuationOptions.ExecuteSynchronously
(注意:这是一个实现细节;不要依赖于这个行为).
Most likely, it would be the thread pool thread that completes the Task
returned by Task.Delay
. This is because await
uses TaskContinuationOptions.ExecuteSynchronously
(note: this is an implementation detail; do not depend on this behavior).
我说最有可能";因为 在某些情况下 ExecuteSynchronously
没有同步执行.这是优化提示,而不是要求.这也适用于 await
之前的延续部分:它最有可能(不是肯定)在完成前件的线程上运行任务.
I say "most likely" because there are situations where ExecuteSynchronously
doesn't execute synchronously. It's an optimization hint, not a requirement. This also applies to the part of your continuation before the await
: it is most likely (not definitely) running on the thread that completed the antecedent task.
但我听说 SynchronziationContext 不流经 ContinueWith 方法调用?是真的吗?
but I've heard that SynchronziationContext does not flow through ContinueWith method calls? Is that true?
ContinueWith
在任务级别工作.它有一些流经 TaskScheduler.Current
的逻辑(IMO 是使这个 API 变得危险的一个方面),但不知道 SynchronizationContext
.
ContinueWith
works at the task level. It has some logic for flowing through TaskScheduler.Current
(which IMO is one aspect that makes this API dangerous), but has no awareness of SynchronizationContext
.
这篇关于TaskContinuations 与异步 lambdas 同步运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!