System.Threading.Task
Added in .NET 4.0
A Task represents an asynchronous operation.
What is the Task Parallel Library ?
This replaces the earlier Processor and Thread classes with a new class called Task.
The System.Threading.Tasks namespace has been designed for executing multi-threaded applications on machines that have multiple core processors.
The new Task class has a higher level of abstraction than the Thread.
Asynchronous implementation is easy in a task, using' async' and 'await' keywords.
A task can return a result
A task can have multiple processes happening at the same time.
A task can have multiple processes happening at the same time.
A task can support cancellation through the use of cancellation tokens
link - learn.microsoft.com/en-us/dotnet/standard/parallel-programming/
link - learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl
These are much more powerful than the Backgroundworker
partitioning of the work,
taking care of state management,
scheduling threads on the thread pool,
using callback methods
enabling continuation or cancellation of tasks.
Data and task parallelism
Schedule threads in a thread pool
Ability to use all available cores
Cancellation of work
Partitioning of work
A task is a lightweight object that manages a parallelism unit of work
Tasks use the CLRs thread pool which avoids creating a new thread.
Methods
Task.Run - Queues the specified work to run on the ThreadPool and returns a task or Task<TResult> handle for that work.
Task.RunSynchronously - Runs the Task synchronously on the current TaskScheduler.
Task.Start - Starts the Task.
Creating
Task Class
This is a replacement for the ThreadPool.QueueUserWorkItem
For non generic task classes we call Task.Factory.StartNew and we pass in a delegate of the target method
System.Threading.Task myTask = new System.Threading.Task( () => System.Console.WriteLine("My new task") );
myTask.Start();
Factory.StartNew
Alternatively you could use this to create and start in one operation
System.Threading.Task.Factory.StartNew( () => System.Console.WriteLine("My new task") );
Task myTask = new Task( () => System.Console.WriteLine("hello") );
myTask.RunSynchronously();
Task Creation Options
LongRunning
PreferFairness
AttachedToParent
Cancelling
Tasks use background threads from the thread pool.
To cancel a task you need to pass a special cancellation token to the task factory.
System.Threading.Cancellation.TokenSource tokenSource = new System.Threading.CancellationTokenSource();
System.Threading.Task.Factory.StartNew( () => System.Console.WriteLine("my new task"), tokenSource.token);
System.Threading.Thread.Sleep(2500);
tokenSource.Cancel();
Task<Result>
This is a replacement for the asynchronous delegates
This is the generic version of the Task class
This enables us to receive a return value from the task after it has finished executing
Async - Await
Added in .NET 4.6
Lets you log exceptions without blocking the current thread
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext