c# - 使用 Parallel.ForEach 和 Task.Factory.StartNew 之间的主要区别是什么

标签 c# asp.net .net asp.net-mvc parallel-processing

我正在开发一个 asp.net mvc-4 Web 应用程序..但我不确定使用这两种方法对列表进行迭代和启动 WebClient() 调用之间有什么区别:-

方法1

Parallel.ForEach(photos,new ParallelOptions { MaxDegreeOfParallelism = 7 }, p =>
                            {
         ResourceAccountListInfo resourceAccountListInfo = new ResourceAccountListInfo();
         WebClient wc = new WebClient();               

         var json =  wc.DownloadString(p.url);
         resourceAccountListInfo = JsonConvert.DeserializeObject<ResourceAccountListInfo>(json);
         if (resourceAccountListInfo.operation.Details.CUSTOMFIELD.Count > 0)
                    {
                        List<CUSTOMFIELD> customfield = resourceAccountListInfo.operation.Details.CUSTOMFIELD.Where(a =>
                                 a.CUSTOMFIELDLABEL.ToLower() == "name"
                                ).ToList();
                        if (customfield.Count == 1)
                        {
                            PMresourcesOnly.Add(resourceAccountListInfo.operation.Details);

                        }

                    }
            //code goes here
                            });

方法2

   foreach (Photo p in photos)
                        {
            Task.Factory.StartNew(() =>
                        {
            ResourceAccountListInfo resourceAccountListInfo = new ResourceAccountListInfo();
            WebClient wc = new WebClient();

            var json =  wc.DownloadString(p.url);
            resourceAccountListInfo = JsonConvert.DeserializeObject<ResourceAccountListInfo>(json);
                     if (resourceAccountListInfo.operation.Details.CUSTOMFIELD.Count > 0)
                    {
                        List<CUSTOMFIELD> customfield = resourceAccountListInfo.operation.Details.CUSTOMFIELD.Where(a =>
                                 a.CUSTOMFIELDLABEL.ToLower() == "name"
                                ).ToList();
                        if (customfield.Count == 1)
                        {
                            PMresourcesOnly.Add(resourceAccountListInfo.operation.Details);

                        }

                    }
            //code goes here
                                });
                        }

谢谢

最佳答案

Parallel.ForEach 为迭代集合提供了便利。 尽管可以在循环内调用 Task.Factory.StartNew,但它本身没有任何内容表明它将在循环中使用。您可能只开始一项任务。

因为 Parallel.ForEach 假定一组并行操作,所以它为您提供了ParallelOptions,允许您指定并发操作的数量在该一个循环的范围内。这很有用,因为在某些情况下我们可能不关心有多少并发操作,但在其他情况下我们可能会关心。看看this answer其中描述了使用信号量来限制并发任务的数量。设置 MaxDegreeOfParallelism 比使用信号量要容易得多。

此外,Parallel.ForEach 返回一个 ParallelLoopResult,它使您可以了解循环中的所有操作。要获得对任务集合的相同可见性,需要做更多的工作。您还可以通过一个方法调用来停止循环,而不是停止多个任务。

关于c# - 使用 Parallel.ForEach 和 Task.Factory.StartNew 之间的主要区别是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088347/

相关文章:

c# - 如何避免在设计时紧密绑定(bind)的用户控件中重置属性?

c# - 没有 Controller 的路由和 url 中的操作

c# - 是锁定实例还是基于成员

c# - SMTP 凭据密码编码问题

c# - 如何将方程式转换为单个变量的公式?

c# - MvcMailer 可以在类库中使用吗?

c# - 从 Web.Config 引用命名空间

asp.net - 无法访问已关闭的文件

用于显示数据的 asp.net MVC GridView 替代方案

c# - 使用 BeginInvoke 运行委托(delegate)方法