c# - Use Parallel.ForEach or fill List<Task> by foreach -


i trying use multi-threading. want run list of task.there 2 choice me, write 2 code below:

 list<task> tasklist=new list<task>();         foreach (var item in itemlist)         {             tasklist.add(task.factory.startnew(()=> dotasks(item,new random().next(15,40))));         }         task.waitall(tasklist.toarray());         console.writeline("all tasks done!"); 

and this:

 parallel.foreach(itemlist, item => dotasks(item, new random().next(15, 40)));         console.writeline("all tasks done!"); 

my questions:
better in performance?
parallel.foreach have threadpool manage opened tasks?
there way better?

parallel.foreach better choice. reuse current thread execute dotasks, total number of threads less in first case. in addition, start execution while in case foreach there delay between moment when created tasks , moment when scheduler run first task.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -