Programming in C# Using Tasks

Using Work items to a thread pool is useful but it has its shortcomings. No inbuilt way to know when the task is complete and what the return value is.

Tasks are Objects that represent some work that should be done. The task can also return an object.

Tasks can be used to make your APP more responsive. It means a windows app can be more responsive and a Web app can process more than one app at once.

A task scheduler is responsible for starting a task and managing it, by default it uses threads to execute a task.

Executing a task makes sense if you want to keep the UI responsive or if you have larger tasks that would be better off splitting between different processors.

task.Wait() is equivalent to t.Join as it waits until the task is finished before exiting the application

Continue with. This runs a after the main thread has run.

Adding Child Tasks to Parent Tasks

Tasks can be attached as sub tasks of a parent task, this can be useful when multiple tasks are required to run as part of a parent task, before continuing with a new task.

TaskFactory

These can in effect set up parameters for tasks that are the same. Sub tasks can then be attached to to task factory that share the same properties.

WaitAll.

This can be used to wait for mulitiple tasks to complete before continue processing.

You load up a task object with multiple tasks and then do a

Task.WaitAll(tasks)

This will wait and exit when all the tasks have run.

Task.WhenAll

This runs a continuation method after all tasks are finished.

Task.WaitAny

This runs when any of the tasks complete