考虑以下代码片段:
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,该怎么办?这种结构有可能吗?
答案 0 :(得分:5)
如果存在具有SyncrhonizationContext的特定线程,例如UI线程,然后你可以做。例如。
// 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()
}
但通常并行任务将从线程池开始。