难道Delay(0)总要得到内联?根据我的经验,它确实:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static async Task Test()
{
await Task.Yield();
Console.WriteLine("after Yield(), thread: {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(0);
Console.WriteLine("after Delay(0), thread: {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Delay(100);
Console.WriteLine("after Delay(100), thread: {0}", Thread.CurrentThread.ManagedThreadId);
}
static void Main(string[] args)
{
Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
Test().Wait();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个控制台应用程序,因此线程池用于继续.输出:
Main thread: 11
after Yield(), thread: 7
after Delay(0), thread: 7
after Delay(100), thread: 6
Run Code Online (Sandbox Code Playgroud) 在我当前的项目中,我有一段代码,在将其简化为我遇到问题的地方后,它看起来像这样:
private async Task RunAsync(CancellationToken cancel)
{
bool finished = false;
while (!cancel.IsCancellationRequested && !finished)
finished = await FakeTask();
}
private Task<bool> FakeTask()
{
return Task.FromResult(false);
}
Run Code Online (Sandbox Code Playgroud)
如果我不等待就使用此代码,无论如何我最终都会阻塞:
// example 1
var task = RunAsync(cancel); // Code blocks here...
... // Other code that could run while RunAsync is doing its thing, but is forced to wait
await task;
// example 2
var task = RunAsync(cancelSource.Token); // Code blocks here...
cancelSource.Cancel(); // Never called
Run Code Online (Sandbox Code Playgroud)
在实际项目中,我实际上并没有使用 FakeTask,通常会有一些 Task.Delay 在那里等待,所以大部分时间代码实际上并没有阻塞,或者只是有限的迭代次数.
然而,在单元测试中,我使用了一个模拟对象,它几乎可以完成 FakeTask …