Task.Factory.StartNew with async lambda and Task.WaitAll

后端 未结 4 1512
小鲜肉
小鲜肉 2020-11-27 07:59

I\'m trying to use Task.WaitAll on a list of tasks. The thing is the tasks are an async lambda which breaks Tasks.WaitAll as it never waits.

4条回答
  •  一向
    一向 (楼主)
    2020-11-27 08:17

    Task.Factory.StartNew doesn't recognise async delegates as there is no overload that accepts a function returning a Task.

    This plus other reasons (see StartNew is dangerous) is why you should be using Task.Run here:

    tasks.Add(Task.Run(async () => ...
    

提交回复
热议问题