如何将foreach转换为Parallel.ForEach? [英] How can I convert a foreach to Parallel.ForEach?

查看:59
本文介绍了如何将foreach转换为Parallel.ForEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换:

  foreach(委托中的NotifyCollectionChangedEventHandler处理程序){...} 

要像这样

  Parallel.ForEach(委托中的NotifyCollectionChangedEventHandler处理程序){...} 

解决方案

您可以这样做:

  Parallel.ForEach(代理人,处理程序=>{//你的东西}); 

考虑以下示例

  List< string>list =新的List< string>(){"ABC","DEF","EFG"};Parallel.ForEach(list,str =>{Console.WriteLine(str);}); 

您可能还会看到:如何:编写简单的Parallel.ForEach循环

how to convert:

  foreach (  NotifyCollectionChangedEventHandler handler in delegates) {
            ...
  }

To somthing like This

 Parallel.ForEach(    NotifyCollectionChangedEventHandler handler in delegates) {
  ... 
 }

解决方案

You can do:

Parallel.ForEach(delegates, handler => 
{ 
//your stuff 
});

Consider the following example

List<string> list = new List<string>()
{
    "ABC",
    "DEF", 
    "EFG"
};

Parallel.ForEach(list, str =>
{
    Console.WriteLine(str);
});

You may also see: How to: Write a Simple Parallel.ForEach Loop

这篇关于如何将foreach转换为Parallel.ForEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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