0

I work on an engine programmed in C# that creates and executes .NET 4.5 Tasks. Within these tasks code is being executed from different APIs. One of them being the Tabular Object Model (TOM).

I want to be able to cancel a running Task and my problem is that the function of TOM i call within the task, has no possibility to receive a CancelationToken.

Code here:

Task t = Task.Run( () => 
{
   // may check cancelation token status before 

   TomApi.CallSomeMethod(); // cannot abort this very call

   // may check cancelation token status after 
});

So my problem is, when some user wants to cancel that running Task t i have no possibility to call cancel on it.

For methods that support async I do it like this:

Task t = Task.Run( () => 
{
   TomApi.CallSomeMethodAsync(token);
});

I know also that I can check the CancelationToken status in the code, but my point is that a call within the task is already running and I could only check for that status before or after that function call

tuxmania
  • 906
  • 2
  • 9
  • 28
  • Can you edit a question into a question? How to cancel non-cancelable method or what do you ask? – Sinatr Jul 16 '18 at 09:58
  • @Sinatr - i do see a very clear question. – bommelding Jul 16 '18 at 10:00
  • I don't understand why it should be hard to do it. It is possible to cancel a Threat object why not a task. – tuxmania Jul 16 '18 at 10:01
  • @bommelding, I don't. Is the question about canceling `t` or about canceling `CallSomeMethod()`? – Sinatr Jul 16 '18 at 10:01
  • If `TomApi.CallSomeMethod();` itself cannot be cancelled there's not a great deal you can do – p3tch Jul 16 '18 at 10:02
  • _"It is possible to cancel a Threat"_ Yes, but all advice is: don't do that. It used to be unsafe, only fixed in Fx 4.0 or so. – bommelding Jul 16 '18 at 10:03
  • And `Task`s run on the ThreadPool so those are borrowed threads: don't mess with them. Step 1 would be to use `Thread`s, and then yes you can call Abort(). – bommelding Jul 16 '18 at 10:04
  • @Sinatr - "about canceling t or about canceling CallSomeMethod()" there is very little difference between the two, is there? – bommelding Jul 16 '18 at 10:05
  • Let's assume you want to cancel `t` : *"i have no possibility to call cancel on it"* - yes you have. You can cancel it using token before or after calling `CallSomeMethod()` (over which you have no control). Do it. It's good. And it's all you can do. If you want to cancel `CallSomeMethod()` itself and there is no api support (in case of synchronous call it should be some kind of multi-threading: signals, pulsing, etc.) - then you are in a bad luck. Unless you manage to create isolated environment (application domain?) you shouldn't try to do anything. Period. – Sinatr Jul 16 '18 at 10:10
  • *"I don't understand why it should be hard to do it."* That's quite an underestimation of the problem. There is no inherently safe cancellation of a task from the outside. There is no safe way for outside code to know the internal state of the execution you want to cancel, so cancellation is best left to the execution itself. If you are in async code the code you execute should be designed to allow cancellation or be left to execute to the end. – Sefe Jul 16 '18 at 10:11
  • @p3tch, tuxmania: do note that the accepted answer there is a little dated, it is about Fx 3.5. But `Thread.Abort()` is still very scary, especially when TomApi (could) call unmanaged code. – bommelding Jul 16 '18 at 10:13
  • Note that if the API you want to execute has some asnyhronous methods, it is likely that (if it is well-designed) the methods that can don't accept the cancellation token are exuting fast enough for the token to neot be needed. – Sefe Jul 16 '18 at 10:17
  • @bommelding - Do you have any reference to aborting a thread being safe now? I still thought it was not. – Enigmativity Jul 16 '18 at 10:38
  • @Enigmativity - On MSDN the Fx 3.5 edition had a dire warning: DO NOT USE. The disappearing of that warning is the best ref I've got. Still, [the manual](https://learn.microsoft.com/en-us/dotnet/standard/threading/destroying-threads) is cautious and the generail idea is that it's a sign of a bad design. – bommelding Jul 16 '18 at 10:59
  • Ok I understand it is not possible. So basically I have to wait/hope that the TOM API will implement async versions of its methods so I can pass the CancelationToken to it. – tuxmania Jul 16 '18 at 11:21
  • @tuxmania - Or push the code out through a separate Process that you can destroy. – Enigmativity Jul 16 '18 at 12:18
  • You can arrange to abandon it (cancel the continuation, just ignore the results). But aborting non-cooperative code is not so easy, or even safe. – bommelding Jul 16 '18 at 14:14

0 Answers0