簡體   English   中英

Async.Start 是否保證完成?

[英]Is Async.Start guaranteed to finish?

我找不到任何文檔。

我想問在 F# 中使用Async.Start是否保證完成,即使調用者或主線程在完成之前退出。 具體來說,我想在處理每個輸入后將大量數據記錄到磁盤上,並想知道我是否可以簡單地使用 Async.Start 。

非常感謝。

在 .NET 中有兩種螺紋 前台線程使進程保持活動狀態。 Async.Start在線程池上運行計算。 線程池線程是后台線程。 所以運行異步操作通常不會完成。

以下程序打印Starting , Exiting main / async trueT2 done

open System.Threading

[<EntryPoint>]
let main _ =

    printfn "Starting"

    let t1 = new Thread(fun () ->
        Thread.Sleep 10000
        printfn "T1 done")
    t1.IsBackground <- true
    t1.Start()

    let t2 = new Thread(fun () ->
        Thread.Sleep 1000
        printfn "T2 done")
    t2.IsBackground <- false
    t2.Start()

    Async.Start (async {
        printfn "async %b" Thread.CurrentThread.IsBackground
        do! Async.Sleep 10000
        printfn "Async done"
    })

    printfn "Exiting main"
    0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM