Async.StartWithContinuations<'T> 方法 (F#)
从当前操作系统线程开始立即运行异步计算。 完成操作之后,调用这三个延续的其中一个。
命名空间/模块路径: 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 8,Windows 7,Windows server 2012中,Windows server 2008 R2
版本信息
F#核心库版本
支持:2.0,4.0,可移植