What are the possible reasons for the final asynchronous {try ... finally ...} not being called? Rx Incolved
I have something like this:
let a =
async {
try
do! Async.AwaitTask someTask
finally
// clean up
}
Async.Start (a, cancellationTokenSource.Token)
When the task awaiting in a
completes, the block is executed finally
and the cleanup is done, but when the async a
is canceled because it cancellationTokenSource
gets Cancel
called, the cleanup is not done. In fact, I suspect it a
continues to work.
I don't really have a clue, so the initial guess goes here: "No, I really guess."
Edit
The problem seems to be with the task expected a
. If someTask
:
Async.Ignore (Async.AwaitIAsyncResult <| Task.Delay(5000))
then no problem :) But! if the task is like this:
vm.Finished.Select(ignore).FirstAsync().ToTask()
( vm
is the view model. Finished
event. Basically, I want it to a
finish either because of cancellation or because of what Finished
happened in vm
.)
Then a
resists to be canceled or finally just skipped, I don't know which one.
source to share
You need to transfer CancellationToken
to ToTask()
, otherwise it cannot be canceled.
Use this ToTask overload .
source to share
Cancellation in .Net is collaborative. This means that once a task is started, signal transmission CancellationToken
has no actual effect unless you control it.
To enable cancellation, you either need to check CancellationToken.IsCancellationRequested
inside the actual operation, and if true, complete it. Or you can call CancellationToken.ThrowIfCancellationRequested
to throw an exception that completes the operation.
source to share