STATHREAD在F#异步工作流程
-
12-09-2019 - |
题
考虑下面的代码片断:
let t1 = async { return process1() } let t2 = async { return process2() } let t3 = async { return windowsProcess() } let execute = [ t1; t2; t3 ] |> Async.Parallel |> Async.RunSynchronously |> ignore
什么在windowsProcess需要STATHREAD的情况下怎么办?这可能与这种结构?
解决方案
如果有与SyncrhonizationContext,例如一个特定的线程UI线程,那么你可以做e.g。
// earlier on the UI thread...
let syncCtxt = System.Threading.SynchronizationContext.Current
// then define t3 as
let t3 = async {
do! Async.SwitchToGuiThread(syncCtxt)
// now we're running on the UI thread until the next async 'bind' point
return windowsProcess()
}
但一般并行任务将在线程池启动。
不隶属于 StackOverflow