为什么我在这里看不到ConfigureAwait(false)和ConfigureAwait(true)之间的区别



我正试图为我在async-await上所做的报告创建概念验证。

我做了一个代码样本

using System;
using System.Threading.Tasks;
using System.Threading;
public class Program
{
static async Task CreateATaskThatPrintsWhichThread()
{
Console.WriteLine("Thread on line right before the bottom await = {0}", Thread.CurrentThread.ManagedThreadId);
await Task.Run(() => 
{
Task.Delay(500);
Console.WriteLine("Thread on line inside bottom task action = {0}", Thread.CurrentThread.ManagedThreadId);                         
});
Console.WriteLine("Thread on line right before the bottom await = {0}", Thread.CurrentThread.ManagedThreadId);
}
public static void Main()
{
Task.Run(async () =>
{
Console.WriteLine("Thread on line right before the bottom await = {0}", Thread.CurrentThread.ManagedThreadId);
Task printStuff = CreateATaskThatPrintsWhichThread();
printStuff.ConfigureAwait(true);
await printStuff;
Console.WriteLine("Thread on line right before the bottom await = {0}", Thread.CurrentThread.ManagedThreadId);
}).Wait();
}
}

我在.NET Fiddle上反复运行,结果是零星的:

Thread on line right before the bottom await = 14
Thread on line right before the bottom await = 14
Thread on line inside bottom task action = 28
Thread on line right before the bottom await = 28
Thread on line right before the bottom await = 28

Thread on line right before the bottom await = 28
Thread on line right before the bottom await = 28
Thread on line inside bottom task action = 28
Thread on line right before the bottom await = 28
Thread on line right before the bottom await = 28

Thread on line right before the bottom await = 28
Thread on line right before the bottom await = 28
Thread on line inside bottom task action = 53
Thread on line right before the bottom await = 53
Thread on line right before the bottom await = 28

等等。当我更改为`printStuff.ConfigureAwait(false);

Thread on line right before the bottom await = 99
Thread on line right before the bottom await = 99
Thread on line inside bottom task action = 87
Thread on line right before the bottom await = 36
Thread on line right before the bottom await = 99
Thread on line right before the bottom await = 45
Thread on line right before the bottom await = 45
Thread on line inside bottom task action = 36
Thread on line right before the bottom await = 36
Thread on line right before the bottom await = 45
Thread on line right before the bottom await = 25
Thread on line right before the bottom await = 25
Thread on line inside bottom task action = 12
Thread on line right before the bottom await = 12
Thread on line right before the bottom await = 25

有人能告诉我结果"应该"是什么吗,或者帮助我在.NET Fiddle上创建一个更好的例子?

(我也很困惑,因为我认为Task.Run内部的行基本上可以保证在后台线程上执行)

注释涵盖了大部分内容,但由于还没有答案,我认为您想要的答案是,您错误地得出结论,.ConfigureAwait(true)意味着在await之后,代码将在与以前相同的线程上继续。

这在某些情况下是正确的,特别是那些具有某种形式的消息/调度程序循环的情况。在这些情况下,当预延续代码完成时,捕获的SynchronizationContext用于将请求发布回调度器,以便在调度器下一次能够获取延续并在其线程上继续执行它。循环通常是连续快速旋转的,为UI问题提供服务,一旦注册了post,它将使用它的一个迭代来执行延续。

控制台应用程序没有这样的循环,也没有SynchronizationContext,因此执行延续的任务被排队到线程池中,任何可用的线程都会执行它。你偶尔会很幸运,最终会得到与延续之前相同的线程,但这只是偶然的。

最新更新