STATHREAD作为F#中的异步工作流 [英] STATHREAD as async workflow in F#
本文介绍了STATHREAD作为F#中的异步工作流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码片段:
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的情况下该怎么办?这种构造有可能吗?
What to do in the case the windowsProcess requires a STATHREAD? Is this possible with this construction?
推荐答案
如果存在带有SyncrhonizationContext的特定线程,例如UI线程,那么您可以执行例如
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.
这篇关于STATHREAD作为F#中的异步工作流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文