Run multiple instances of same method asynchronously?

前端 未结 6 2051
情深已故
情深已故 2021-01-03 09:32

My requirement is quite weird.

I have SomeMethod() which calls GetDataFor().

public void SomeMethod()
{
    for(int i = 0         


        
6条回答
  •  没有蜡笔的小新
    2021-01-03 10:04

    I would instead add each of the tasks to a collection and then await on the entire collection AFTER the loop.

    Awaiting inside of a loop like that will create lots of continuations and more overhead than desirable including waiting for each call to finish before continuing the loop I believe.

    Take a look at awaiting Task.WaitAll instead.

    If instead the value of each task is important to process then look at awaiting Task.WhenAll and then read the results of each Task into your return collection.

提交回复
热议问题