There are many ways in which we can create and run a task using the TPL. In this section, we will try to understand all of these approaches and do a comparative analysis wherever we can. First, you need to add a reference to the System.Threading.Tasks namespace:
using System.Threading.Tasks;
We will try to create a task using the following approaches:
- The System.Threading.Tasks.Task class
- The System.Threading.Tasks.Task.Factory.StartNew method
- The System.Threading.Tasks.Task.Run method
- System.Threading.Tasks.Task.Delay
- System.Threading.Tasks.Task.Yield
- System.Threading.Tasks.Task.FromResult<T> Method
- System.Threading.Tasks.Task.FromException and Task.FromException<T>
- System.Threading.Tasks.Task.FromCancelled and Task.FromCancelled<T>