Async.StartWithContinuations<'T> 方法 (F#)

更新: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

请参见

参考

Control.Async 类 (F#)

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

修订记录

Date

修订记录

原因

2010 年 7 月

添加了代码示例。

信息补充。