Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Async.RunSynchronously method block?

Tags:

f#

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?

like image 252
user3587180 Avatar asked Jun 12 '26 02:06

user3587180


1 Answers

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).

like image 105
Tomas Petricek Avatar answered Jun 14 '26 15:06

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!