dotnetnoob
dotnetnoob

Reputation: 11330

How do I cancel third party task?

I a have a third party task running in the following code:

var ts = new CancellationTokenSource();
var ct = ts.Token;

var node = Task<IResponseAdapter>.Factory.StartNew(() =>
{
    return NewRequest(n.RequestClass, n.MinCommission, n.MaxCommission).Submit();

}, ct);

As you can see, the method call is the only thing inside the task and the whole reason for being inside a task is so that I can cancel it if it times out. A number of third party methods are run sequentially, so it's important to monitor misbehaving/non-responsive jobs and terminate them so that the next job then run.

Elsewhere in my code I have

var completed = task.Wait(timeoutValue);

if(!completed) tokensource.Cancel();

However, I believe this doesn't actually cancel the task, and only provides a method for accessing it.

So how can I stop the third party webservice method from running?

Upvotes: 1

Views: 611

Answers (1)

usr
usr

Reputation: 171178

You certainly cannot stop a method that is already running on another computer (through a webservice). The cancellation token for tasks can only stop the task from starting when the token is set. Once the task has started there is no way back for the same reason that Thread.Abort should (almost) never be used.

Upvotes: 1

Related Questions