1 | initial version |
To terminate a task using C#, you can use the CancellationTokenSource class.
Here's an example:
CancellationTokenSource cts = new CancellationTokenSource();
Task task = Task.Run(() => {
// your code here
}, cts.Token);
cts.Cancel();
try {
task.Wait();
}
catch (AggregateException ae) {
if (ae.InnerExceptions.Any(e => e is OperationCanceledException)) {
// task was cancelled
}
else {
// handle other exceptions
}
}