对于初学者来说,这种方法可以简化为:
public IAsyncEnumerable<Job> GetByPipeline(int pipelineId)
{
return context.Jobs
.Where(job => job.Pipeline.Id == pipelineId)
.AsAsyncEnumerable();
}
or even
public IAsyncEnumerable<Job> GetByPipeline(int pipelineId)
=> context.Jobs
.Where(job => job.Pipeline.Id == pipelineId)
.AsAsyncEnumerable();
该方法不执行任何操作job
所以它不需要迭代它。
消除
如果实际使用该方法会怎样job
,取消令牌应该用在哪里?
让我们稍微清理一下该方法。等价的是:
public async IAsyncEnumerable<Job> GetByPipeline(
int pipelineId,
[EnumeratorCancellation] CancellationToken ct = default)
{
//Just a query, doesn't execute anything
var query =context.Jobs.Where(job => job.Pipeline.Id == pipelineId);
//Executes the query and returns the *results* as soon as they arrive in an async stream
var jobStream=query.AsAsyncEnumerable();
//Process the results from the async stream as they arrive
await foreach (var job in jobStream.WithCancellation(ct).ConfigureAwait(false))
{
//Does *that* need cancelling?
DoSometingExpensive(job);
}
}
可查询的query
不运行任何东西,它代表查询。是不需要取消的。
AsAsyncEnumerable()
, AsEnumerable()
, ToList()
etc execute查询并返回一些结果。ToList()
等消耗所有结果,而As...Enumerable()
方法仅在请求时才产生结果。查询无法取消,As_Enumerable()
除非要求,否则方法不会返回任何内容,因此不需要取消。
await foreach
将迭代整个异步流,因此如果我们希望能够中止它,我们do需要传递取消令牌。
最后,是否DoSometingExpensive(job);
需要取消吗?它是否太昂贵以至于我们希望在花费太长时间的情况下能够摆脱它?或者我们可以等到它完成后再退出循环吗?如果需要取消,也需要 CancellationToken。
配置等待
最后,ConfigureAwait(false)
不涉及取消,并且可能根本不需要。没有它,每次之后await
执行返回到原始同步上下文。在桌面应用程序中,这意味着 UI 线程。这就是我们可以在异步事件处理程序中修改 UI 的原因。
If GetByPipeline
在桌面应用程序上运行并想要修改 UI,则必须删除ConfugureAwait
:
await foreach (var job in jobStream.WithCancellation(ct))
{
//Update the UI
toolStripProgressBar.Increment(1);
toolStripStatusLabel.Text=job.Name;
//Do the actual job
DoSometingExpensive(job);
}
With ConfigureAwait(false)
,在线程池线程上继续执行,我们can't触摸用户界面。
库代码不应该影响执行的恢复方式,因此大多数库使用ConfigureAwait(false)
并将最终决定权留给 UI 开发人员。
If GetByPipeline
是一个库方法,请使用ConfigureAwait(false)
.