Async.RunSynchronously方法是否阻塞? [英] Does Async.RunSynchronously method block?

查看:59
本文介绍了Async.RunSynchronously方法是否阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

是的, Async.RunSynchronously 块.一个简单的例子:

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"

这将打印:

Main starting
Async starting
Async done
Main done

它与C#中的 task.RunSynchronously 大致相似-尽管可能存在一些细微的差异(F#工作流将使用线程池执行,而主线程被阻塞并等待完成而等效的C#可能实际上在当前线程上运行该工作,这更类似于F#中的 StartImmediate -但是不会等待).

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

这篇关于Async.RunSynchronously方法是否阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆