更新:2010 年 7 月
取消條件沒有任何特定的取消通知語彙基元的情況下啟動非同步計算的最新集合將會引發。 取代全域 CancellationTokenSource物件與新全域語彙基元來源的任何非同步計算,建立此之後沒有 任何特定的取消通知語彙基元。
命名空間/模組路徑: Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member CancelDefaultToken : unit -> unit
// Usage:
Async.CancelDefaultToken ()
範例
下列範例顯示如何在 Windows Form 應用程式中建立 cancellable 的非同步作業。 它也顯示如何使用 Async.CancelDefaultToken來取消 作業。
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 月 |
加入程式碼範例。 |
資訊加強。 |