考虑以下代码片段:
让 t1 = async { 返回 process1() }
让 t2 = async { 返回 process2() }
让 t3 = async { 返回 windowsProcess() }
让执行 =
[t1; t2; t3]
|> 异步并行
|> Async.RunSynchronously
|> 忽略
如果windowsProcess需要STATHREAD怎么办?这种结构有可能吗?
考虑以下代码片段:
让 t1 = async { 返回 process1() }
让 t2 = async { 返回 process2() }
让 t3 = async { 返回 windowsProcess() }
让执行 =
[t1; t2; t3]
|> 异步并行
|> Async.RunSynchronously
|> 忽略
如果windowsProcess需要STATHREAD怎么办?这种结构有可能吗?
如果有一个带有 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()
}
但通常并行任务将在线程池中启动。