Async.CancelDefaultToken 方法 (F#)
針對最近已啟動但沒有任何特定取消語彙基元的一組非同步計算,引發取消條件。針對在此之後建立但沒有任何特定取消語彙基元的非同步計算,以新的全域語彙基元來源取代全域的 CancellationTokenSource 物件。
**命名空間/模組路徑:**Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member CancelDefaultToken : unit -> unit
// Usage:
Async.CancelDefaultToken ()
範例
下列範例示範如何在 Windows Forms 應用程式中建立可取消的非同步作業。這也顯示如何使用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 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2
版本資訊
F# 核心程式庫版本
支援版本:2.0, 4.0,可攜式執行檔 (PE)。