.net - Async.AwaitEvent does not cancel after invocation of underlying event -
.net - Async.AwaitEvent does not cancel after invocation of underlying event -
i'm trying understand async.awaitevent behavior. according documentation:
creates asynchronous computation waits single invocation of cli event adding handler event. 1 time computation completes or cancelled, handler removed event.
and remarks section:
the computation respond cancellation while waiting event. if cancellation occurs, , cancelaction specified, executed, , computation continues wait event. if cancelaction not specified, cancellation causes computation cancel immediately.
i not specify kind of cancel action , hence expect event processed 1 time , happened, handler should unsubscribed. if run sample, can see event handled on , on again.
open microsoft.fsharp.control allow timer = new system.timers.timer(2000.0) timer.autoreset <- true timer.start() allow fn = async { allow timerevent = async.awaitevent timer.elapsed _ in [1..10] let! x = timerevent printfn "elapsed event occurred @ %o" x.signaltime } fn |> async.runsynchronously output:
elapsed event occurred @ 19.06.2014 23:46:21 elapsed event occurred @ 19.06.2014 23:46:23 elapsed event occurred @ 19.06.2014 23:46:25 elapsed event occurred @ 19.06.2014 23:46:27 elapsed event occurred @ 19.06.2014 23:46:29 elapsed event occurred @ 19.06.2014 23:46:31 elapsed event occurred @ 19.06.2014 23:46:33 elapsed event occurred @ 19.06.2014 23:46:35 elapsed event occurred @ 19.06.2014 23:46:37 elapsed event occurred @ 19.06.2014 23:46:39 is behavior differ documented one? if not, why?
timerevent of type async<elapsedeventargs>. in general, async<'something> representation of async computation run in future, perchance repeatedly. build let! how async<'something> run.
this similar how let f() = printfn "wibble" defines function f print "wibble" each time run.
so what's happening here each time let! x = timerevent runs, you're subscribing event 1 time again , waiting happen.
once event occurs, particular subscription cancelled, command continues past let! printfn, , around loop 1 time again , new subscription made.
.net events asynchronous f#
Comments
Post a Comment