打破parallel.foreach? [英] Break parallel.foreach?

查看:188
本文介绍了打破parallel.foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何摆脱的<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx\">parallel.for循环?

我有一个pretty复杂的声明,如下所示:

  Parallel.ForEach&LT; ColorIndexHolder&GT;(ColorIndex.AsEnumerable()
    新的动作&LT; ColorIndexHolder&GT;((ColorIndexHolder元)=&GT;
    {
        如果(Element.StartIndex&下; = I和;&放大器; Element.StartIndex + Element.Length&GT; = 1)
        {
            发现= TRUE;
            打破;
        }
    }));

使用并行类,我可以通过远优化这个过程。然而;我无法弄清楚如何打破并行循环?在突破; 语句引发以下语法错误:


  

没有封闭循环外面中断或继续



解决方案

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break.aspx\"><$c$c>ParallelLoopState.Break方法:

  Parallel.ForEach(列表,
    (I,状态)= GT;
    {
       state.Break();
    });

或者你的情况:

  Parallel.ForEach&LT; ColorIndexHolder&GT;(ColorIndex.AsEnumerable()
    新的动作&LT; ColorIndexHolder,ParallelLoopState&GT;((ColorIndexHolder元,ParallelLoopState状态)=&GT;
    {
        如果(Element.StartIndex&下; = I和;&放大器; Element.StartIndex + Element.Length&GT; = 1)
        {
            发现= TRUE;
            state.Break();
        }
    }));

How do I break out of an parallel.for loop?

I have a pretty complex statement which looks like the following:

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
    new Action<ColorIndexHolder>((ColorIndexHolder Element) =>
    {
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
        {
            Found = true;
            break;
        }
    }));

Using parallel class, I can optimize this process by far. However; I cannot figure out how to break the parallel loop? The break; statement throws following syntax error:

No enclosing loops out of which to break or continue

解决方案

Use the ParallelLoopState.Break method:

 Parallel.ForEach(list,
    (i, state) =>
    {
       state.Break();
    });

Or in your case:

Parallel.ForEach<ColorIndexHolder>(ColorIndex.AsEnumerable(),
    new Action<ColorIndexHolder, ParallelLoopState>((ColorIndexHolder Element, ParallelLoopState state) =>
    {
        if (Element.StartIndex <= I && Element.StartIndex + Element.Length >= I)
        {
            Found = true;
            state.Break();
        }
    }));

这篇关于打破parallel.foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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