更新:2010 年 7 月
直接從目前的作業系統執行緒開始,傳回非同步計算。 在作業完成時,呼叫三個接續項目的其中一個。
命名空間/模組路徑: Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member StartWithContinuations : Async<'T> * ('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) * ?CancellationToken -> unit
// Usage:
Async.StartWithContinuations (computation, continuation, exceptionContinuation, cancellationContinuation)
Async.StartWithContinuations (computation, continuation, exceptionContinuation, cancellationContinuation, cancellationToken = cancellationToken)
參數
computation
型別:Async<'T>要執行的非同步計算。
continuation
型別:'T -> unit成功時呼叫的函式。
exceptionContinuation
型別:exn -> unit發生例外狀況時呼叫的函式。
cancellationContinuation
型別:OperationCanceledException -> unit取消時呼叫的函式。
cancellationToken
型別:CancellationToken選擇性的取消通知語彙基元,以關聯計算。 如果未提供這個參數,則會使用預設值。
備註
如果提供了沒有取消語彙基元,就會使用預設的取消通知語彙基元。
範例
下列程式碼範例說明如何使用 Async.StartWithContinuations。
open System.Windows.Forms
let bufferData = Array.zeroCreate<byte> 100000000
let async1 (label:System.Windows.Forms.Label) filename =
Async.StartWithContinuations(
async {
label.Text <- "Operation started."
use outputFile = System.IO.File.Create(filename)
do! outputFile.AsyncWrite(bufferData)
},
(fun _ -> label.Text <- "Operation completed."),
(fun _ -> label.Text <- "Operation failed."),
(fun _ -> label.Text <- "Operation canceled."))
let form = new Form(Text = "Test Form")
let button1 = new Button(Text = "Start")
let button2 = new Button(Text = "Start Invalid", Top = button1.Height + 10)
let button3 = new Button(Text = "Cancel", Top = 2 * button1.Height + 20)
let label1 = new Label(Text = "", Width = 200, Top = 3 * button1.Height + 30)
form.Controls.AddRange [| button1; button2; button3; label1 |]
button1.Click.Add(fun args -> async1 label1 "longoutput.dat")
// Try an invalid filename to test the error case.
button2.Click.Add(fun args -> async1 label1 "|invalid.dat")
button3.Click.Add(fun args -> Async.CancelDefaultToken())
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
請參閱
參考
Microsoft.FSharp.Control 命名空間 (F#)
變更記錄
日期 |
History |
原因 |
|---|---|---|
|
2010 年 7 月 |
加入程式碼範例。 |
資訊加強。 |