STATHREAD as async workflow in F#

社会主义新天地 提交于 2019-12-10 22:47:33

问题


Consider the following code fragment:

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

What to do in the case the windowsProcess requires a STATHREAD? Is this possible with this construction?


回答1:


If there's a particular thread with a SyncrhonizationContext, e.g. the UI thread, then you could do 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() 
    }

but in general parallel tasks will start in the threadpool.



来源:https://stackoverflow.com/questions/1115132/stathread-as-async-workflow-in-f

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