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
}
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-09-07 11:00:00 +0000
Seen: 14 times
Last updated: Jul 06 '22
What is the process for obtaining and recording all Linq To Db queries in a database table?
What is the process of accessing a URL within a Class Component?
What is the process of separating Swing listeners?
What is the process for invoking a method in a class by its name while passing arguments?
What is the process for connecting an external table to an EF-core model?
What is the process for incorporating std::hash into a class template?
What is the process for adding a class path permanently to the Mac terminal?
How can the __iter__ method of the cursor class in psycopg2 be expanded?