首页 > 解决方案 > c# - 异步/等待超出预期顺序

问题描述

我试图了解我编写的示例的结果。这里是:

    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();
                }
            }
        }
    }

我希望计数器产生一个输出,它们在 2 行后切换。相反,从 counter2 开始,当它计数到 5 时,它就像是在自己让步一样。我得到的是以下内容:

counter1: 0
counter1: 1
counter1: 2 is divisible by 2. Yielding
         counter2: 0
         counter2: 1
         counter2: 2 is divisible by 2. Yielding
                     counter3: 0
                     counter3: 1
                     counter3: 2 is divisible by 2. Yielding
counter1: 3
counter1: 4 is divisible by 2. Yielding
         counter2: 3
         counter2: 4 is divisible by 2. Yielding
counter1: 5
counter1: 6 is divisible by 2. Yielding
         counter2: 5
                     counter3: 3
counter1: 7
counter1: 8 is divisible by 2. Yielding
         counter2: 6 is divisible by 2. Yielding
                     counter3: 4 is divisible by 2. Yielding
         counter2: 7
         counter2: 8 is divisible by 2. Yielding
                     counter3: 5
                     counter3: 6 is divisible by 2. Yielding
                     counter3: 7
                     counter3: 8 is divisible by 2. Yielding
counter1: 9
                     counter3: 9
                     counter3: 10 is divisible by 2. Yielding
counter1: 10 is divisible by 2. Yielding
         counter2: 9
         counter2: 10 is divisible by 2. Yielding

为什么订单没有维护?

编辑1:

我已经添加了threadId。似乎线程一直在切换。这是为什么?我的平台是 Windows 上的 .NET Core 控制台应用程序。

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

标签: c#asynchronousasync-await

解决方案


您在控制台应用程序中,因此默认情况下没有同步上下文。在第一次await运行之后,它的继续将被安排在另一个线程池线程上。在主线程上,第二个计数器启动,当它再次到达时await,它将产生另一个“分支”。然后这些将并行运行,您将失去预期的订单。

如果您希望一次只运行一个任务,则需要使用同步上下文或仅调度到一个线程的任务调度程序。


推荐阅读