C# regarding running multiple task
-
State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks
var stage1 = Task.Run(() =>
{}); var stage2 = Task.Run(() => { }); // Block until both tasks have completed. // This makes this method prone to deadlocking. // Consider using 'await Task.WhenAll' instead. Task.WaitAll(stage1, stage2);
-
State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks
var stage1 = Task.Run(() =>
{}); var stage2 = Task.Run(() => { }); // Block until both tasks have completed. // This makes this method prone to deadlocking. // Consider using 'await Task.WhenAll' instead. Task.WaitAll(stage1, stage2);
Mou_kol wrote:
State1 & state2 will run parallel or State1 will run first and later State2 will run ?
Yes. Or perhaps 2 will run first and 1 later. There is no way to control what happens with tasks of equal priority: they exist on separate threads, and will be executed when the OS decides it is appropriate , and as a core becomes available. That means that the execution order of the tasks is indeterminate: it cannot be predicted or relied upon.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks
var stage1 = Task.Run(() =>
{}); var stage2 = Task.Run(() => { }); // Block until both tasks have completed. // This makes this method prone to deadlocking. // Consider using 'await Task.WhenAll' instead. Task.WaitAll(stage1, stage2);
Task.Run will execute the contained code on the ThreadPool. The TreadPool will allow a number of tasks to run in parallel. So as long as you have not reached this limit and there is a CPU core available, they will execute in parallel. If you have reached the limit, they might run one by one - and they might delay execution until a thread becomes available. This is typically not something you need to think about, but worth knowing if you have long running operations or if you are queueing a lot of operations on the thread pool - for example looping over a collection and running something for each item. If you need to do the latter, look into "Task Parallel Library"[^]
-
State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks
var stage1 = Task.Run(() =>
{}); var stage2 = Task.Run(() => { }); // Block until both tasks have completed. // This makes this method prone to deadlocking. // Consider using 'await Task.WhenAll' instead. Task.WaitAll(stage1, stage2);
-
State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks
var stage1 = Task.Run(() =>
{}); var stage2 = Task.Run(() => { }); // Block until both tasks have completed. // This makes this method prone to deadlocking. // Consider using 'await Task.WhenAll' instead. Task.WaitAll(stage1, stage2);
If I were you and for some reason I needed to read/write multiple files in parallel (though Gerry is right when it comes to writing files to traditional hard disk drives), instead of the methods you use, I would simplify things by using the Parallel.For or the Parallel.Foreach method.