2

Say I want to frequently check the status of a long-running asynchronous operation while doing a bunch of other work. Is it possible to check whether an Async computation expression is completed (just like with Task's IsCompleted property)?

In other words, is there a way of interacting with an Async other than via the methods provided by its computation expression builder or should I use Tasks instead?

nphx
  • 1,426
  • 3
  • 19
  • 30
  • 3
    Unlike `Task`, F# `Async` is referentially transparent; it doesn't start executing until you explicitly start it, and it's done when it's done. For instance, if you call `Async.RunSynchronously`, you know that it's _not_ completed until it returns. If you, instead, use `Async.Start`, you're going to need some sort of synchronisation flag that the asynchronous workflow can update when it's done. – Mark Seemann Oct 30 '16 at 09:24
  • 1
    I reckon using a mutable flag is more trouble than `Async.StartAsTask` with `IsCompleted` :). Thanks for your input. – nphx Oct 30 '16 at 09:40
  • 5
    To add to Mark's good reply, a given `Async` value can be started several times, unlike a `Task`. If you come from C#, you could see `Async<'T>` as being conceptually equivalent to `Func>`. So the concept of "being started" doesn't really apply to the `Async` itself. `StartAsTask` does seem to be the right way to go for your use case. – Tarmil Oct 30 '16 at 12:06

0 Answers0