내 F# (FsXaml/뒤에 코드) 응용 프로그램을 사용하고 싶 진행 표시줄 없이 이용의 배경을 작업자로서 내가 할 수 있습니다. 기반으로 기사를 인터넷에서(연결은 여기에서),나를 사용하여 비동기 워크플로우를 경험할 수 있습니다.
내가 만들어 코드 기반의(일부 확장)에 예에서 앞서 언급한 문서지만,그것은 작동하지 않았으로 예상된다. 현재 실 UI(thread)는 여전히 차단되지 않은 경우 비동기 코드가 있었다. 아 전환하면 백그라운드 스레드가 발생합니다. 바로 활성화한 후에만 긴 실행 중인 작업이 완료되었습니다. 을 제거하 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