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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.