Task.WaitAll not waiting for task to complete

前端 未结 4 715
攒了一身酷
攒了一身酷 2021-02-07 07:10

While trying to figure out the new (maybe not so new now, but new to me, anyway) Task asynchronous programming in C#, I ran into a problem that took me a bit to fig

4条回答
  •  野性不改
    2021-02-07 08:12

    Here, Task.WaitAll waits for the outer task and not the inner task. Use Task.Run to not have nested tasks. That's the best practices solution. Another solution is to also wait for the inner task. For example:

    Task testTask = Task.Factory.StartNew(
        async (obj) =>
            {
                ...
            }
        ).Unwrap();
    
    
    

    Or:

    testTask.Wait();
    testTask.Result.Wait();
    

    提交回复
    热议问题