共用方式為


Async.StartImmediate 方法 (F#)

更新:2010 年 8 月

直接從目前的作業系統執行緒開始,傳回非同步計算。

命名空間/模組路徑: Microsoft.FSharp.Control

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
static member StartImmediate : Async<unit> * CancellationToken option -> unit

// Usage:
Async.StartImmediate (computation)
Async.StartImmediate (computation, cancellationToken = cancellationToken)

參數

  • computation
    型別:Async<unit>

    要執行的非同步計算。

  • cancellationToken
    型別:CancellationToken

    選擇性的取消通知語彙基元,以關聯計算。 如果未提供這個參數,則會使用預設值。

備註

如果未提供取消語彙基元,則會使用預設的取消語彙基元。

範例

下列程式碼範例示範如何使用 Async.StartImmediate開始非同步的計算上 目前的執行緒。 通常,非同步作業需要更新使用者介面應該永遠在 UI 執行緒執行。 當您的非同步作業需要藉由更新 UI, Async.StartImmediate是比較好的選擇,比 Async.Start ,啟動非同步的作業,執行緒集區執行緒上的開始。


open System.Windows.Forms

let bufferData = Array.zeroCreate<byte> 100000000

let async1 (button : Button) =
     async {
       button.Text <- "Busy"
       button.Enabled <- false
       let context = System.Threading.SynchronizationContext.Current
       do! Async.SwitchToThreadPool()
       use outputFile = System.IO.File.Create("longoutput.dat")
       do! outputFile.AsyncWrite(bufferData)
       do! Async.SwitchToContext(context)
       button.Text <- "Start"
       button.Enabled <- true
     }


let form = new Form(Text = "Test Form")
let button = new Button(Text = "Start")
form.Controls.Add(button)
button.Click.Add(fun args -> Async.StartImmediate(async1 button))
Application.Run(form)

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Control.Async 類別 (F#)

Microsoft.FSharp.Control 命名空間 (F#)

變更記錄

日期

History

原因

2010 年 8 月

改良的程式碼範例。

資訊加強。