Skip to content

Instantly share code, notes, and snippets.

@somapatrik
Last active November 11, 2021 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somapatrik/d58eee51f70e4aa178daca10d547d751 to your computer and use it in GitHub Desktop.
Save somapatrik/d58eee51f70e4aa178daca10d547d751 to your computer and use it in GitHub Desktop.
Parallel execution of several tasks
// 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