Pipeline pattern using in parallel ForEach C#
my unique doubt here is what task do I have to add to my task list to wait for the process to finished. or well if you have a different approach will be helpful, I basically want to iterate a collections of IDs where on each ID I have two process one depend on the other result. first task is a long running task over my local network. and depending on the result I will add to "MyCollection" a new instance if that task return true. based in the pipeline pattern., now
so, to control all the tasks to wait until they finished is my doubt., please see example and comment, thanks.
List<Task> tasks = new List<Task>();
BlockingCollection<MyType> MyCollection = new BlockingCollection<MyType>(IdsCollection.Count);
Parallel.ForEach(IdsCollection,
(Id) =>
{
Task<bool> task1 = Task.Factory.StartNew(() =>
{
//long running task over the network
return DoSmething(Id);
});
Task task1_1 = task1.ContinueWith((isValid) =>
{
if (isValid.Result)
MyCollection.Add(new MyType(Id));
});
// ??? do I need to Add both or the task1 ??
tasks.Add(task1_1);
}
);
Task.WaitAll(tasks.ToArray());
Yes, adding task1_1
is enough, there is no reason to also explicitly wait for task1
.
But:
Task
for both would work just as well. List<T>
is not thread-safe, so you can't just access it from inside Parallel.ForEach()
like this. This means that your code is likely to throw, or, worse, produce incorrect results. Parallel.ForEach()
if all you're going to do is to in it start a Task
and set up its continuation. All you're gaining from using it here is some overhead. await
it, it's more efficient. 上一篇: 将项目添加到正在迭代的集合中,还是等效?
下一篇: 并行使用的管道模式ForEach C#