Is there a general way to gain external control over the execution of an arbitrary asynchronous workflow, for suspension and resumption? Akin to a CancellationToken but more like a SuspendResumeToken where one could simply do things like:
let suspendResumeToken = new SuspendResumeToken()
Async.Start(longRunningAsync, suspendResumeToken=suspendResumeToken)
...
// meanwhile
async {
do! suspendResumeToken.SuspendAsync()
printfn "Suspended longRunningAsync!"
do! suspendResumeToken.ResumeAsync()
printfn "Resumed longRunningAsync!"
}
I could emulate the behavior for recursive or iterated functions, by checking such a token at every iteration or recursion step, but since async workflows have natural yield points, it would be natural to have that built-in, as a general way to control async scheduling.
Implementing something like your
SuspendResumeToken
is easily done using events. You can define an event, await it in the computation and then trigger it from the outside to resume the computation:There is no way to get this automatically at yield-points in
async
, but you could define your own computation builder which is exactly likeasync
, except that it inserts the check in theBind
operation (and possibly some other places - you'd have to think about where exactly you want to do this):Note that this only checks around
!
operations likedo!
so I had to insert aSleep
in the example to make it work.