Writing to the console using Task.Run() fails

后端 未结 2 1761
粉色の甜心
粉色の甜心 2021-02-19 20:51

A colleague of mine found an issue with our code and it took a while to hunt down exactly what was happening, but it can be best demonstrated by this simple example:

<         


        
2条回答
  •  时光说笑
    2021-02-19 21:46

    Actually, the first case doesn't fail. "Hello World" appears just before the Application ends. This is a classical Race Condition. In the first case,Console.ReadKey() from the main thread beats the task, and in the second case, the task wins. Unfortunately, I cannot tell you Exactly why writing the empty string makes the task win.

提交回复
热议问题