user3587180
user3587180

Reputation: 1397

Does Async.RunSynchronously method block?

From the documentation it appears that the Async.RunSynchronously runs the async computation and awaits its result. I've also read that it's similar to await in C#. I'm curious if this blocks the thread until it's run to completion?

Upvotes: 8

Views: 638

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

Yes, Async.RunSynchronously blocks. A simple illustration:

let work = async {
  printfn "Async starting"
  do! Async.Sleep(1000)
  printfn "Async done" }

printfn "Main starting"
work |> Async.RunSynchronously
printfn "Main done"

This will print:

Main starting
Async starting
Async done
Main done

It is roughly similar to task.RunSynchronously in C# - although there might be some subtle differences (the F# workflow will be executed using a thread pool while the main thread is blocked and waits for the completion while the C# equivalent might actually run the work on the current thread which is more akin to StartImmediate in F# - which however does not wait).

Upvotes: 16

Related Questions