STATHREAD作为F#中的异步工作流

2 parallel-processing f# sta

请考虑以下代码片段:

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的情况下该怎么办?这种结构有可能吗?

Bri*_*ian 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() 
    }
Run Code Online (Sandbox Code Playgroud)

但通常并行任务将在线程池中启动.