Reputation: 6489
Is this possible to get current running Task
and cancel it in other places which there is no access to Task object? Let's say I started a Task in classA
and I want to cancel it in classB
, is there anyway to find current Task and cancel it?
Upvotes: 1
Views: 1387
Reputation: 171178
No, there is no notion of a current Task
. There are arbitrarily many tasks running at the same time. Even "running" is not well defined. Is new TaskCompletionSource<object>().Task
running or is it not?
Create a cancellable CancellationToken
and pass it to the task to make it self-cancel. Pass the underlying CancellationTokenSource
to the code that wants to cancel.
Upvotes: 4