Last active
November 11, 2021 10:33
-
-
Save somapatrik/d58eee51f70e4aa178daca10d547d751 to your computer and use it in GitHub Desktop.
Parallel execution of several tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create tasks, must use Task.Run() not new Task() | |
Task expireTask = Task.Run(()=> { ExpireCheck(); }); | |
Task agingTask = Task.Run(() => { AgingCheck(); }); | |
Task fifoTask = Task.Run(() => { FIFOCheck(); }); | |
Task novalidTask = Task.Run(() => { NoValidCheck(); }); | |
Task nolifeTask = Task.Run(() => { NoLifeCheck(); }); | |
// Add them to list | |
List<Task> tasks = new List<Task>(); | |
tasks.Add(expireTask); | |
tasks.Add(agingTask); | |
tasks.Add(fifoTask); | |
tasks.Add(novalidTask); | |
tasks.Add(nolifeTask); | |
// Prepare WhenAll | |
Task t = Task.WhenAll(tasks.ToArray()); | |
// Tasks will be executed parallelly | |
await t; // await will execute all task but code execution will CONTINUE | |
t.Wait(); // This will WAIT for all task to complete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment