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();
}
}
}
这是一个控制台应用程序,因此线程池用于继续。输出:
Main thread: 11
after Yield(), thread: 7
after Delay(0), thread: 7
after Delay(100), thread: 6