c#-异步/等待超出预期顺序
我正试图了解我编写的一个示例的结果。这是:c#-异步/等待超出预期顺序,c#,asynchronous,async-await,C#,Asynchronous,Async Await,我正试图了解我编写的一个示例的结果。这是: class Program { static async Task Main(string[] args) { var counter1 = new Counter("counter1",string.Empty); var counter2 = new Counter("counter2"," "); var cou
class Program
{
static async Task Main(string[] args)
{
var counter1 = new Counter("counter1",string.Empty);
var counter2 = new Counter("counter2"," ");
var counter3 = new Counter("counter3"," ");
await Task.WhenAll(counter1.Count(), counter2.Count(),
counter3.Count());
}
}
public class Counter
{
private string _name;
private string _prefix;
public Counter(string name, string prefix)
{
_name = name;
_prefix = prefix;
}
public async Task Count()
{
for (int i = 0; i <= 10; i++)
{
Console.Write($"{_prefix}{_name}: {i}");
if (i % 2 == 0 && i != 0)
{
Console.WriteLine($" is divisible by 2. Yielding");
await Task.Yield();
}
else
{
Console.WriteLine();
}
}
}
}
为何不维持秩序
编辑1:
我已经添加了threadId。似乎线程一直在切换。为什么呢?我的平台是Windows上的.NET核心控制台应用程序
counter1: 0. ThreadId: 1
counter1: 1. ThreadId: 1
counter1: 2. ThreadId: 1 is divisible by 2. Yielding
counter2: 0. ThreadId: 1
counter2: 1. ThreadId: 1
counter2: 2. ThreadId: 1 is divisible by 2. Yielding
counter3: 0. ThreadId: 1
counter3: 1. ThreadId: 1
counter3: 2. ThreadId: 1 is divisible by 2. Yielding
counter1: 3. ThreadId: 3
counter1: 4. ThreadId: 3 is divisible by 2. Yielding
counter3: 3. ThreadId: 4
counter3: 4. ThreadId: 4 is divisible by 2. Yielding
counter3: 5. ThreadId: 4
counter3: 6. ThreadId: 4 is divisible by 2. Yielding
counter1: 5. ThreadId: 3
counter1: 6. ThreadId: 3 is divisible by 2. Yielding
counter3: 7. ThreadId: 4
counter3: 8. ThreadId: 4 is divisible by 2. Yielding
counter3: 9. ThreadId: 4
counter3: 10. ThreadId: 4 is divisible by 2. Yielding
counter1: 7. ThreadId: 3
counter1: 8. ThreadId: 3 is divisible by 2. Yielding
counter1: 9. ThreadId: 3
counter1: 10. ThreadId: 3 is divisible by 2. Yielding
counter2: 3. ThreadId: 5
counter2: 4. ThreadId: 5 is divisible by 2. Yielding
counter2: 5. ThreadId: 5
counter2: 6. ThreadId: 5 is divisible by 2. Yielding
counter2: 7. ThreadId: 5
counter2: 8. ThreadId: 5 is divisible by 2. Yielding
counter2: 9. ThreadId: 5
counter2: 10. ThreadId: 5 is divisible by 2. Yielding
您位于控制台应用程序中,因此默认情况下没有同步上下文。在第一个
wait
运行后,它的继续将被安排在另一个线程池线程上。在主线程上,第二个计数器启动,当它再次到达await
时,它将产生另一个“分支”。这些将并行运行,您将失去预期的顺序
如果希望一次只运行一个任务,则需要使用同步上下文或只调度一个线程的任务计划程序。请将
控制台.WriteLine(System.Threading.thread.CurrentThread.ManagedThreadId.ToString())
添加到循环中并发布结果。此外,请标记平台/应用程序类型,例如.NET Core vs.Framework和WinForms vs.ASP.NET等,因为它们都略有不同。可能重复。我希望Task.Whalll为:1。采取其中一种方法并执行它
,它不是这样工作的。当您调用.Count()
时,该方法开始执行Task.WhenAll
只是等待,但我确实在这一行中调用了.Count():wait Task.WhenAll(counter1.Count(),counter2.Count(),counter3.Count());然后呢?这严格等同于var task1=counter1.Count();var task2=counter2.Count();var task3=counter3.Count();等待任务。WhenAll(任务1、任务2、任务3)代码>也许这样更容易理解
counter1: 0. ThreadId: 1
counter1: 1. ThreadId: 1
counter1: 2. ThreadId: 1 is divisible by 2. Yielding
counter2: 0. ThreadId: 1
counter2: 1. ThreadId: 1
counter2: 2. ThreadId: 1 is divisible by 2. Yielding
counter3: 0. ThreadId: 1
counter3: 1. ThreadId: 1
counter3: 2. ThreadId: 1 is divisible by 2. Yielding
counter1: 3. ThreadId: 3
counter1: 4. ThreadId: 3 is divisible by 2. Yielding
counter3: 3. ThreadId: 4
counter3: 4. ThreadId: 4 is divisible by 2. Yielding
counter3: 5. ThreadId: 4
counter3: 6. ThreadId: 4 is divisible by 2. Yielding
counter1: 5. ThreadId: 3
counter1: 6. ThreadId: 3 is divisible by 2. Yielding
counter3: 7. ThreadId: 4
counter3: 8. ThreadId: 4 is divisible by 2. Yielding
counter3: 9. ThreadId: 4
counter3: 10. ThreadId: 4 is divisible by 2. Yielding
counter1: 7. ThreadId: 3
counter1: 8. ThreadId: 3 is divisible by 2. Yielding
counter1: 9. ThreadId: 3
counter1: 10. ThreadId: 3 is divisible by 2. Yielding
counter2: 3. ThreadId: 5
counter2: 4. ThreadId: 5 is divisible by 2. Yielding
counter2: 5. ThreadId: 5
counter2: 6. ThreadId: 5 is divisible by 2. Yielding
counter2: 7. ThreadId: 5
counter2: 8. ThreadId: 5 is divisible by 2. Yielding
counter2: 9. ThreadId: 5
counter2: 10. ThreadId: 5 is divisible by 2. Yielding