Is there a way to cancel background task made with HostingEnvironment.QueueBackgroundWorkItem
?
There is CancellationToken
which notifies if tasks was cancelled but how can i do it?
Refering to https://msdn.microsoft.com/en-us/library/dd997396(v=vs.110).aspx
A successful cancellation involves the requesting code calling the CancellationTokenSource.Cancel method
OK. Where can i get access to CancellationTokenSource
?
The signature of
HostingEnvironment.QueueBackgroundWorkItem
is:This means that a work item gets access to a
CancellationToken
. But I don't think that's useful in your case. From the documentation:If you want to cancel the
workItem
based on some other condition, you can use a separateCancellationToken
, which you create from aCancellationTokenSource
. For example, to cancel a work item if it doesn't start within 10 seconds:This will technically still start the work item, even if the
CancellationToken
is cancelled, but it won't do anything.Note that cancellation is always cooperative. This means that if you have a long-running work item, and you want to cancel it in the middle of its execution, you will need to keep checking the
CancellationToken
periodically.After few trials i came up with the soulution:
Update:
Indeed, task is not needed. Thanks svick for bringing that up. Here is a bit more detailed code sample without task.