ในของฉัน 'F'# (FsXaml/รหัสหลัง) โปรแกรม,ฉันต้องการจะใช้ความคืบหน้าโดยไม่มีบาร์ที่ utilisation ของพื้นหลังของงานที่ผมทำใน C#. จากบทความเกี่ยวกับอินเตอร์เน็ต(การเชื่อมต่อ อยู่)ฉันพยายามจะใช้ asynchronous workflows.
ฉันสร้างรหัสจาก(จะมีขยาย)บนตัวอย่างใน aforementioned บทความแต่มันไม่ได้ทำงานที่ฉันคาดไว้เลย ดเธรดปัจจุบัน(ส่วนติดต่อผู้ใช้ด้าย)ยังคงถูกปิดกั้นเหมือนว่าไม่ async รหัสอยู่ที่นั่น ไม่มีการสลับผังต้องเป็นพื้นหลังของเธรดเกิดขึ้น. ที่ต้องเดินหน้าต่อร์บาร์เปิดใช้งานเดียวหลังจากที่นานกำลังปฏิบัติการถูกเสร็จสิ้นแล้ว ลบที่ 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