Async await how to use return values

后端 未结 4 2033
猫巷女王i
猫巷女王i 2021-02-04 00:41

I have a windows service that I have inherited from another developer, it runs very slow and has numerous slow call to the eBay API. I wish to speed it up without too much refac

4条回答
  •  暖寄归人
    2021-02-04 00:49

    If the task returned by Task.WhenAll has completed, that means all of the tasks that you passed to it have completed too. That in turn means that you can use the Result property of each task, with no risk of it blocking.

    string details = additionalProductDetials.Result;
    

    Alternatively, you could await the tasks, for consistency with other async code:

    string details = await additionalProductDetials;
    

    Again, this is guaranteed not to block - and if you later remove the Task.WhenAll for some reason (e.g. you're happy to use the details to kick off another task before you've got the part number collection) then you don't need to change the code.

提交回复
热议问题