C# async/await strange behavior in console app

前端 未结 4 814
臣服心动
臣服心动 2021-01-17 22:33

I built some async/await demo console app and get strange result. Code:

class Program
{
    public static void BeginLongIO(Action act)
    {
        Console.         


        
4条回答
  •  一整个雨季
    2021-01-17 22:45

    Short answer is that LongIOAsync() is blocking. If you run this in a GUI program you will actually see the GUI freezes briefly - not the way async/await is supposed to work. Therefore the entire thing falls apart.

    You need to wrap all the long running operations in a Task then directly await on that Task. Nothing should block during that.

提交回复
热议问题