Updated 23/03/2021
Task List
Running Tasks in parallel is very simple, the collection of tasks below return a string and its workload is simply to create a text file and then delay for x milliseconds to mimic a longer workload.
The complete code for these snippets is at https://github.com/carlpaton/ThreadingDemo/tree/main/src/RunTasksInParallel
Some configuration
1 | var millisecondsTimeout = 2000; |
Create the service that does some work and has the mimic of a longer workload.
1 | public async Task<string> HandleAsync(int millisecondsTimeout) |
Create the task list and populate it with the workload.
Note that adding them to the list is not what starts the tasks, its the File.WriteAllTextAsync
that would have been the trigger.
1 | var tasks = new List<Task<string>>(); |
Then wait for the tasks to all complete
1 | await Task.WhenAll(tasks); |
Task Array
This was adapted from stackoverflow.com examples and is very simliar to the list example above however the tasks are started using Task.Factory.StartNew
.
1 | var listOfSomething = new PopulateListOfSomething(); |