c# - 异步/等待超出预期的顺序

时间:2018-05-27 10:03:26

标签: 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 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

为什么订单没有维护?

EDIT1:

我添加了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

1 个答案:

答案 0 :(得分:3)

您已进入控制台应用程序,因此默认情况下没有同步上下文。在第一个await运行后,其继续将在另一个线程池线程上进行调度。在主线程上,第二个计数器启动,当它到达await时,它将产生另一个&#34;分支&#34;。然后这些将并行运行,您将失去预期的订单。

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