Does Async.RunSynchronously method block?

只愿长相守 提交于 2021-01-27 07:01:38

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/54312750/does-async-runsynchronously-method-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!