Cancel a task

A task can be canceled at runtime. You cannot run  a cancelled task.

Making a task cancellable

You can cancel a task if the value returned from reading its
IsCancellationRequested
property is appropriately managed in the method that executes, usually using conditional instructions.
The
IsCancellationRequested
property can have the following values:
false
Cancellation not requested.
true
Cancellation requested.
To use the property within the method,  indicate the task as the argument of the same method, specifying its type.
In the following example,
myTask
is a task and
ProcessCsvFile()
is the method that executes:
private void ProcessCsvFile(LongRunningTask myTask) { // Code to execute }

Cancel a task

You cancel a task by invoking the
Cancel()
method on the task. See the following example.
myTask.Cancel();
The method sets the value of the task
IsCancellationRequested
property to
True
.
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.