This .NET type is used to schedule a method on the thread pool. We can use Task.Run
to call a method at some point in the future, and ContinueWith
to add another method after that.
With Wait, meanwhile, we can wait for all the methods in our queue to be called. In this way we can chain methods, one after another, on a thread pool.
Consider this example program. It includes the System.Threading.Tasks
namespace. And we invoke Task.Run
in Main()
when it begins.
Task.Run
that calls the useful Method1()
method.ContinueWith
we pass another lambda expression, one that receives a Task argument "task" and calls Method2()
.Task.Run
and ContinueWith
, we impose order on the methods that are added to the method queue (for the thread pool).using System; using System.Threading.Tasks; class Program { static void Main() { // Call Task.Run and invoke Method1. // ... Then call Method2. // Finally wait for Method2 to finish for terminating the program. Task.Run(() => Method1()).ContinueWith(task => Method2()).Wait(); } static void Method1() { Console.WriteLine("::Method1::"); } static void Method2() { Console.WriteLine("::Method2::"); } }::Method1:: ::Method2::
CancellationToken
Suppose we have a long-running task, and we may want to cancel it at some point (end it before it completes). We can use a CancellationTokenSource
and CancellationToken
.
CancellationTokenSource
. We access the Token from it (this is a CancellationToken
).DoSomething()
.DoSomething
receives a CancellationToken
and on each iteration of its loop, it tests for IsCancellationRequested
.TaskCancelledException
).using System; using System.Threading; using System.Threading.Tasks; class Program { static void Main() { // Create CancellationTokenSource. var source = new CancellationTokenSource(); // ... Get Token from source. var token = source.Token; // Run the DoSomething method and pass it the CancellationToken. // ... Specify the CancellationToken in Task.Run. var task = Task.Run(() => DoSomething(token), token); // Wait a few moments. Thread.Sleep(500); // Cancel the task. // ... This affects the CancellationTokens in the source. Console.WriteLine("Main::Cancel"); source.Cancel(); // Wait more. Thread.Sleep(500); } static void DoSomething(CancellationToken token) { // Do something important. for (int i = 0; i < 100; i++) { // Wait a few moments. Thread.Sleep(100); // See if we are canceled from our CancellationTokenSource. if (token.IsCancellationRequested) { Console.WriteLine("Method1 canceled"); return; } Console.WriteLine($"Method1 running... {i}"); } } }Method1 running... 0 Method1 running... 1 Method1 running... 2 Method1 running... 3 Main::Cancel Method1 canceled
We can call Cancel()
on the CancellationTokenSource
from any method—even if the method being run as a task. In the above example, we call Cancel()
in Main
.
Suppose we have a method we want to call, and we want to use its result in a control in a Windows Forms or WPF program.
ContinueWith
that uses thread-safe logic (InvokeRequired
, BeginInvoke
) if needed.Task.Run
sets a property on a Windows program control.With Task, and its static
method Task.Run
, we can start a Task. ContinueWith
allows us to build a sequence of methods—which we can wait to terminate with the Wait()
method.