Reputation: 3038
I am using an external library that has async
methods, but not CancellationToken
overloads.
Now currently I am using an extension method from another StackOverflow question to add a CancellationToken
:
public async static Task HandleCancellation(this Task asyncTask, CancellationToken cancellationToken)
{
// Create another task that completes as soon as cancellation is requested. http://stackoverflow.com/a/18672893/1149773
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
cancellationToken.Register(() =>
tcs.TrySetCanceled(), useSynchronizationContext: false);
Task cancellationTask = tcs.Task;
// Create a task that completes when either the async operation completes, or
// cancellation is requested.
Task readyTask = await Task.WhenAny(asyncTask, cancellationTask);
// In case of cancellation, register a continuation to observe any unhandled exceptions
// from the asynchronous operation (once it completes). In .NET 4.0, unobserved task
// exceptions would terminate the process.
if (readyTask == cancellationTask)
asyncTask.ContinueWith(_ => asyncTask.Exception,
TaskContinuationOptions.OnlyOnFaulted |
TaskContinuationOptions.ExecuteSynchronously);
await readyTask;
}
However the underlying task still executes to completion. This wouldn't be much of a problem, but sometimes the underlying task never completes and consumes 99% of my CPU.
Is there any way to "kill" the task without killing the process?
Upvotes: 18
Views: 18992
Reputation: 43886
Is there any way to "kill" the task without killing the process?
No, there isn't. Quoting from a blog post by Microsoft:
So, can you cancel non-cancelable operations? No.
Even if you kill the process, there is no guarantee that the operation itself will be stopped. A Task
, also known as a promise, is a general construct that represents the completion of some operation, and exposes no information about how and where this operation is performed. For all you know, this operation might be performed on a device driver, on a thread, on a separate process, on a separate machine, or on a remote server at the other side of the world. You can't inject cancellation behavior to an operation that was never designed to be canceled, more than you can inject self-destructing behavior to a flying rocket that was never equipped with self-destructing hardware.
Let's see an example:
public async Task<int> DoStuffAsync()
{
Process p = new();
p.StartInfo.FileName = @"C:\SomeProgram.exe";
p.Start();
await p.WaitForExitAsync();
return p.ExitCode;
}
If you launch the asynchronous DoStuffAsync()
operation and then before the Task
is completed you kill the current process, the SomeProgram.exe will keep running. So assuming that you don't have access to the source code of the DoStuffAsync
and you don't know what it's doing internally, you won't be able to do anything to stop it after it has been launched. What you can do is ask the developer who owns it to release a new version that supports cancellation, or search for a better library.
Upvotes: 0
Reputation: 457207
I am using an extension method from another StackOverflow question
That code is very old.
The modern AsyncEx approach is an extension method Task.WaitAsync
, which looks like this:
var ct = new CancellationTokenSource(TimeSpan.FromSeconds(2)).Token;
await myTask.WaitAsync(ct);
I like how the API ended up because it's more clear that it's the wait that is cancelled, not the operation itself.
Is there any way to "kill" the task without killing the process?
No.
The ideal solution is to contact the authors of the library you're using and have them add support for CancellationToken
.
Other than that, you're in the "cancel an uncancelable operation" scenario, which can be solved by:
Upvotes: 20
Reputation: 6716
The only way I can think of is to change the TaskScheduler
and mange the creation of the threads that are used for the tasks yourself. That is a lot of work.
The basic concept is to create your own implementation of the TaskScheduler
, start a new task with your own scheduler assigned. This way you get your scheduler to be the current one and start your problematic task from this task.
There are still reason that may not work. If the task causing you trouble creates more tasks using the default task scheduler you still got the same problem. (Task.Run does so)
How ever if they are using the async/await
key words your scheduler will remain active.
Now with the scheduler under your own control, you can kill any task by using Thread.Abort.
To get a idea about the implementation afford, you should have a look at the ThreadPoolTaskScheduler
. That is the default implementation of the scheduler.
As I said this is a lot of work, but the only way I can think of to kill task that can't be cancelled.
To get a test running if that even works at all you may only want to implement the behaviour the ThreadPoolTaskScheduler
has for the TaskCreationOptions.LongRunning
option. So spawning a new thread for each task.
Upvotes: 1
Reputation: 911
As you suggest you can cancel a task by passing in a CancellationToken
and then calling Cancel
.
As for how you'd go about triggering that cancellation depends on the nature of your application.
A few possible scenarios
In case 1 you simply cancel the task from your cancel button, for example
private void cancel_Click(object sender, RoutedEventArgs e)
{
...
cts = new CancellationTokenSource();
await MyAsyncTask(cts.Token);
cts.Cancel();
...
}
In case 2 you could start a timer when you start your task and then cancel the task after a set time using CancelAfter
, for example
private void start_Click(object sender, RoutedEventArgs e)
{
...
cts = new CancellationTokenSource();
cts.CancelAfter(30000);
await MyAsyncTask(cts.Token);
...
}
In case 3 you could do something with progress, for example
private void start_Click(object sender, RoutedEventArgs e)
{
...
Progress<int> progressIndicator = new Progress<int>(ReportProgress);
cts = new CancellationTokenSource();
await MyAsyncTask(progressIndicator, cts.Token);
...
}
void ReportProgress(int value)
{
// Cancel if no progress
}
Here are a few useful links Parallel programming, task cancellation, progress and cancellation, cancel tasks after set time, and cancel a list of tasks.
Upvotes: -1