暂停并恢复Parallel.ForEach [英] Pausing and resuming Parallel.ForEach

查看:79
本文介绍了暂停并恢复Parallel.ForEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在某种情况下能够暂停Parallel.ForEach。

I need at some condition to be able pause Parallel.ForEach.

如果发生某些情况,我需要等到所有已经运行的线程完成,然后执行一些操作继续foreach执行

If some condition happened, I need to wait until all already running threads complete, then perform some action the continue foreach execution

Parallel.ForEach(mAFGuidList,               
 newParallelOptions { MaxDegreeOfParallelism = parallelDegree },

 ( m , state)=>{
//-------------------------------

 
Interlocked.Increment(refcallCount);
                        

      if(callCount % 500== 0)

       {

         1. Pause Execution and wait antill started earlier threads are 
         2. Prform some action

         3. Resume execution

  }

//----------------------------


    DoSomeWork();

 

  }






可以这样做吗?

推荐答案

您好YuriKPMG,

Hi YuriKPMG,

感谢您在此发帖。

对于您的问题,您可以使用  ParallelLoopState.Stop方法()和  ParallelLoopState.Break方法()。

For your question, you could use ParallelLoopState.Stop Method () and ParallelLoopState.Break Method ().

ParallelLoopState.Stop 方法()

ParallelLoopState.Stop Method ()

https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.stop.asp x

https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.stop.aspx

ParallelLoopState.Break方法(

ParallelLoopState.Break Method ()

https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break.aspx

这是两个类似的主题供您参考。

Here are two similar threads for your reference.

https:/ /stackoverflow.com/questions/20535368/pausing-and-resuming-a-parallel-foreach

https://stackoverflow.com/questions/8461430/parallel-foreach-c-sharp-pause-and-stop-function / 8461721#8461721

最好的问候,

Wendy


这篇关于暂停并恢复Parallel.ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆