3

In my f# project, I'm calling a c# library that returns me a Task. When I try to do this in my f# project, nothing happens.

let str = getName() |> Async.AwaitTask |> Async.RunSynchronously

However, if I update my code to use an async workfolow, it doesn't hang anymore. What am I doing wrong when calling Async.RunSynchronously?

async {
     let! str = getName() |> Async.AwaitTask
     //Do something with str
}
user3587180
  • 1,317
  • 11
  • 23

1 Answers1

2

In your second example you just build async workflow, but don't actually run it.

It is designed that way so you could define a complex workflow without running every async part of it immediately, but instead run the whole thing when it's good and ready.

In order to run it you need to call Async.RunSynchronously or Async.StartAsTask:

async {
 let! str = getName() |> Async.AwaitTask
 //Do something with str
} |> Async.RunSynchronously

Also, if you do need to work with Tasks, it's probably better to use TaskBuilder.fs instead of Async.

kagetoki
  • 4,339
  • 3
  • 14
  • 17