在我 F# (FsXaml/代码) 应用程序,我想用一个进度条没有使用的背景工作人员为我做的。 基于上文在互联网上(链接是 在这里)时,我试图使用异步的工作流程。
我创建了基于代码(到一些延伸),在该例子在本文前述的,但它没有工作,因为我曾预期的。 目前的螺纹(UI线)仍然受阻,如果没有异步的码在那里。 没有开关的背景线发生。 进度条会被激活唯一后的长期运行的操作已经完成。 除 onThreadPool 功能没有效果。
我的问题是:什么是错误的,在我的代码和如何使它的权利?
type MainWindowXaml = FsXaml.XAML<"XAMLAndCodeBehind/MainWindow.xaml">
type MainWindow() as this =
inherit MainWindowXaml()
//....some code....
let ButtonClick _ =
//....some code....
let longRunningOperation() = //....some long running operation (reading from Google Sheets)....
let progressBar() = this.ProgressBar.IsIndeterminate <- true
let doBusyAsync progress operation =
progress
async
{
do! operation
}
|> Async.StartImmediate
let onThreadPool operation =
async
{
let context = System.Threading.SynchronizationContext.Current
do! Async.SwitchToThreadPool()
let! result = operation
do! Async.SwitchToContext context
return result
}
let asyncOperation progress operation =
async { operation }
|> onThreadPool
|> doBusyAsync progress
(progressBar(), longRunningOperation()) ||> asyncOperation
do
//....some code....
this.Button.Click.Add ButtonClick