Cancel of Task Task
To cancel the c# task, you need to use the CancellationTokenSource class and the CancellationToken structure.
Note: CancellationTokenSource is class type, while CancellationToken is a struct structure.
CancellationToken method inside the task
The task is internally called the ThrowIfCancellationRequested() method of CancellationToken at the appropriate time. This function will throw an exception called OperationCanceledException. Its implementation (Microsoft open source code) is as follows:
public void ThrowIfCancellationRequested() { if (IsCancellationRequested) ThrowOperationCanceledException(); }
The columns below generate a task through the following. The Task task officially achieves the cancellation by throwing an OperationCanceledException exception. The outside of the task is to trigger the cancellation action by calling the Cancel() method of the CancellationTokenSource instance.
The following example is an example of canceling a Task task
using System; using ; using ; class Program { static async Task Main() { var tokenSource2 = new CancellationTokenSource(); CancellationToken ct = ; var tokenSource3 = new CancellationTokenSource(); var task = (() => { // Were we already canceled? (); bool moreToDo = true; while (moreToDo) { // Poll on this property if you have to do // other cleanup before throwing. // Clean up here, then... if() { (); } } },); // Pass same token to . (5000); (); // Just continue on this thread, or await with try-catch: try { await task; } catch (OperationCanceledException e) { ($"{nameof(OperationCanceledException)} thrown with message: {}"); bool eqs = (); ($"Equals' result: {eqs}."); ($"Task's status: {}"); } catch (Exception ex) { (); } finally { (); } (); } } /* The operation was canceled. OperationCanceledException thrown with message: The operation was canceled. Equals' result: False. Task's status: Canceled */
In this example, we created two CancellationTokenSource instances outside the Task, because the CancellationTokenSource class contains a property token of the CancellationToken class, which means that the example contains two CancellationToken instances.
The ThrowIfCancellationRequested() method will also throw the CancellationToken instance that calls it as a parameter.
Therefore, the catch block parameter e contains, so it must be false for the output to be compared equally with making the output.
Also, because the second parameter passed in the() method is also, the Task core framework will compare the CancellationToken instance carried in the OperationCanceledException and the CancellationToken instance passed in the() method. If the two are the same, the Task status status is set to Canceled, indicating that it is successfully cancelled; if the two are different, the Task task will still exit, but the Task status status is set to Faulted, indicating that there is an error.
If you change line 29 to:
},); // Pass same token to .
Then, the result output is as follows:
OperationCanceledException thrown with message: The operation was canceled.
Equals' result: False.
Task's status: Faulted
It seems that the Task task was cancelled, but it was actually exited due to an error. Therefore, the second parameter can be used to ensure that the CancellationToken instance that actually raises the cancellation exception must be consistent with the incoming CancellationToken instance to be successfully cancelled, otherwise the framework will mistakenly believe that you were operating incorrectly and causing the exit. Therefore, the CancellationToken parameter is for more security.
If you change line 29 to an overloaded function without the CancellationToken parameter, the same Faulted result is returned.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.