[英]C# - Parallel.Foreach() for Service Call
我有一个控制台应用程序,它执行下面提到的 3 个步骤,
对于第 2 步,我使用的是Parallel.Foreach() ,它的效果比foreach()好得多。 我已经阅读了很多关于 stackoverflow 的文章和线程,这在这个主题上造成了更多的混乱。 我有几个问题
感谢您花时间和精力帮助我。 谢谢!
public void notificationMethod()
{
List<notify> notifList = new List<notify>();
//step 1
List<orders> orderList = GetNotifs();
try
{
if (orderList.Count > 0)
{
Parallel.ForEach(orderList, (orderItem) =>
{
//step 2
SendNotifs(orderItem);
notifList.Add(new notify()
{
//building list by adding email address along with other information
});
});
if (notifList.Count > 0)
{
int index = 0;
int rows = 10;
int skipRows = index * rows;
int updatedRows = 0;
while (skipRows < notifList.Count)
{
//pagination
List<notify> subitem = notifList.Skip(index * rows).Take(rows).ToList<notify>();
updatedRows += subitem.Count;
//step 3
UpdateDatabase(subitem);
index++;
skipRows = index * rows;
}
}
}
}
catch (ApplicationException ex)
{
}
}
关于使用Parallel.ForEach()
是否有助于提高性能,我也有类似的情况。 但是当我看到下面来自微软的视频时,它让我想到了 select Parallel.ForEach()
仅适用于 CPU 密集型工作负载。 在这种情况下,您的场景将属于 I/O 密集型工作负载,并且可以通过async
/ await
更好地处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.