Running two parallel .foreach () together

I have a background worker that calls parallel threads for list items

doWork() { Parallel.foreach(list1, a=> { while(true) { //do some operations } }); Parallel.foreach(list2, b=> { while(true) { //do some operations } }); } 

If I want to run both of these lists in parallel, should I create another thread as a parent or another way? I get both lists separately, so combining them is not an option. And I run endless loops inside them.

+4
source share
2 answers

You can use tasks to run two loops in separate threads. Use regular foreach loops.

 var t1 = Task.Factory.StartNew(() => { foreach(var a in list1) { //do some operations } }); var t2 = Task.Factory.StartNew(() => { foreach(var a in list1) { //do some operations } }); // This will block the thread until both tasks have completed Task.WaitAll(t1, t2); 
+4
source

Create two separate threads for two foreach loops.

0
source

Source: https://habr.com/ru/post/1445710/


All Articles