简体   繁体   English

C#-Parallel.ForEach和异步组合

[英]C# - Parallel.ForEach and Async combination

I've a problem with parallel.foreach and async combination. 我对parallel.foreach和async组合有问题。 Here is my code - 这是我的代码-

new Thread(() =>{
     //doing some stuff here
     Parallel.ForEach(.....,ParallelOption,async(j,loopState) =>
     {
        //await some stuff here like:
       // HttpResponseMessage res = await httpClient.GetAsync(url);
      // UpdateUI
     }
}).Start();

Now, my problem is how can I be sure of the loop has completed all jobs? 现在,我的问题是如何确定循环已完成所有作业? It just ends in few seconds but UIUpdateing will continue for much more time. 它只是在几秒钟内结束,但是UIUpdateing将继续更多时间。 How its possible to wait for the await httpClient.GetAsync(url) to complete and then update the UI? 如何等待await httpClient.GetAsync(url)完成然后更新UI?

Using async/await for multiple tasks 对多个任务使用异步/等待

return Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));

I don't know what's in your actual loop code. 我不知道您的实际循环代码中有什么。 If you put more up I can make this more specific. 如果您提出更多建议,我可以做得更具体。 Basically though you use WhenAll to await all of the ascnc tasksf 基本上,尽管您使用WhenAll等待所有的ascnc任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM