[英]C# - Parallel.Foreach() for Service Call
I have a console application which does 3 steps as mentioned below,我有一个控制台应用程序,它执行下面提到的 3 个步骤,
For step 2, I am using Parallel.Foreach() and it is working far better than foreach() .对于第 2 步,我使用的是Parallel.Foreach() ,它的效果比foreach()好得多。 I have gone through lot of articles and threads on stackoverflow which is causing more confusion on this topic.
我已经阅读了很多关于 stackoverflow 的文章和线程,这在这个主题上造成了更多的混乱。 I have few questions
我有几个问题
Appreciate your time and effort on helping me with this.感谢您花时间和精力帮助我。 Thank you!
谢谢!
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)
{
}
}
I also had a similar scenario regarding whether using Parallel.ForEach()
would help improve the performance.关于使用
Parallel.ForEach()
是否有助于提高性能,我也有类似的情况。 But when I saw the below video from Microsoft, it gave me an idea to select Parallel.ForEach()
only for CPU intensive workloads.但是当我看到下面来自微软的视频时,它让我想到了 select
Parallel.ForEach()
仅适用于 CPU 密集型工作负载。 In this case, your scenario will fall into I/O intensive workloads and could be handled better by async
/ await
.在这种情况下,您的场景将属于 I/O 密集型工作负载,并且可以通过
async
/ await
更好地处理。
https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-2-Distinguish-CPU-Bound-work-from-IO-bound-work https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-2-Distinguish-CPU-Bound-work-from-IO-bound-work
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.