last day (14 days later) » 

11:23 AM
3
A: How do I cancel a concurrent heavy Task?

Yuval ItzchakovIf you can't control the long running method, then cooperative cancellation isn't going to work. What you can do is offload the heavy job to a different process, and monitor on the process in a background thread: private void MyProcess(CancellationTokenSource cts) { cts.Token.ThrowIfCancella...

 
He could also have done this with a different thread and called thread.Stop() instead of another process
 
@eranotzap Nonononono. Please, don't. Thread.Abort is a very bad idea. Writing abort-safe code is a huge pain, and almost all of the .NET framework is not abort-safe. It's just asking for trouble.
 
There isn't a Stop method declared in the Thread class. If you mean Thread.Abort, google it and you'll get plenty of reasons why not to use it.
 
Thanks for your answer, but i have many Task and i can't to hold all Task references in my app! i want to cancel that from tasks by CTS because CTS sent to all tasks.
 
Just a timer would be enough to monitor the process periodically. The cancellation token handling code actually hurts, as it will prevent cancellation if the token is signalled
 
11:23 AM
You don't have to check the process has exited like that - processes are wait handles, so you can just create a new SafeWaitHandle(process.Handle) and await that. Or, you could simply use the Exited event (very easy to await using TaskCompletionSource).
 
@Luaan But you also want to monitor the cancellation of the token, not only the exiting of the process.
 
@PanagiotisKanavos Task.Delay is a timer. It will work fine.
 
@PanagiotisKanavos Task.Delay uses a timer internally.
 
Yeah, but that's what Task.WhenAny is for :))
 
@Luaan Thread.Abort is a bad idea because it's non-coooperative. The OP has non-cooperative code to run. If the OP could change the original code, he could easily pass the cancellation token. Thread.Abort is no more evil than Process.Kill
 
11:23 AM
@Luaan How would you apply WhenAny here?
 
@PanagiotisKanavos Oh, it's far more evil. For one, it's only supposed to be used when an AppDomain is being unloaded - that's why most of .NET framework isn't abort-safe when you're not unloading an AppDomain. Second, it will quite easily interrupt releasing unmanaged resources. Process.Kill is immediate, and much safer - it will not corrupt the state of your process, unlike Thread.Abort. And Thread.Abort doesn't work when you're outside of managed code.
 
@PanagiotisKanavos How is the cancellation handling code going to prevent cancellation? It isn't handled in the delegate, but outside in the Cancel method.
@PanagiotisKanavos Eric lippert explains it well here. Thread.Abort has more culprit then it seems.
 
There's a few ways, but you'll have to write a few helper classes / methods. The simplest way would be using a TaskCompletionSource that would be triggered on the CancellationToken cancellation - either through the Register method, or through the wait handle. In the simplest form, that would be something like Task WaitAsync(CancellationToken token) { var tcs = new TaskCompletionSource<bool>(); token.Register(() => tcs.TrySetResult(true)); return tcs.Task; }. Depending on your use case, you might want to handle disposing the token registration and a few other things as well
 
@YuvalItzchakov the entire Task.Run and calls to ThrowIfCancellationRequested aren't needed since you are simply starting an external process. The loop (or even a timer) is also not needed to cancel the process - just use CancellationToken.Register to pass the code to execute when cancellation is requested.
 
@PanagiotisKanavos Yes i'm modifying the code right now to use Register. About the OCE, that's a matter of flavor. He may throw it or not, depends if he wants to get notified about the cancellation or he doesn't care. If he does care and has some logic that deals with cancellation, it will be important to notify.
 
11:23 AM
@Luaan how is the TCS going to help? If the OP could modify the code, he could pass the token itself. Are you trying to convert MyProcess and waiting for completion to an asynchronous operation?
 
@PanagiotisKanavos He could convert Process.Start to get notified by registering to the Exited event. But he doesn't need that here.
 
@YuvalItzchakov there are too many SO questions about cases where Exited doesn't fire. BTW, why start the process inside a Task? Spawning a process is fast.
 
@PanagiotisKanavos That's just a simple way to await any wait handle or similar event-like constructs. This way, you can use Task.WhenAny to await both the process waithandle (or the Exited event) and the cancellation token, continuing execution when either of those fires / completes - which will allow the Process.Kill to run when the cancellation is started. You wouldn't need await to do this, but as long as we're using await, why bother ourselves with manual thread pool notifications?
 
@PanagiotisKanavos Two step refactoring :)
@Luaan He would you create a Task out of a CancellationToken in order to monitor if it has been canceled?
 
I've posted the code a few comments up (assuming you meant "how would you ..."). It's just a bare-bone Task, nothing that would use threads or anything. It's a shame .NET framework doesn't simply have WaitHandle.WaitAsync or something, it would be rather useful. You could use SemaphoreSlim, but that's way harder to understand IMO.
 
11:23 AM
@Luaan But a SafeWaitHandle doesn't expose any Task based waiting. Using SemaphoreSlim is a major overkill here.
 
I really don't understand what you're trying to get at here. You can use the same method I used for CancellationToken above (using ThreadPool.RegisterWaitForSingleObject). I said that it's a shame that .NET FW doesn't have the WaitAsync on it's own - it's rather easy to code in. Either you don't understand something I'm saying, or I have no idea what you're saying :D
 

last day (14 days later) »