Why is Async.RunSynchronously hanging?

前提是你 提交于 2019-12-12 13:02:01

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/52229404/why-is-async-runsynchronously-hanging

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