Async.StartAsTask<'T> 方法 (F#)
在執行緒集區中執行計算。傳回 Task,一旦計算結束 (產生結果、擲回例外狀況或被取消) 此項目就會完成對應的狀態。如果未提供取消語彙基元,則會使用預設的取消語彙基元。
**命名空間/模組路徑:**Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member StartAsTask : Async<'T> * ?TaskCreationOptions * ?CancellationToken -> Task<'T>
// Usage:
Async.StartAsTask (computation)
Async.StartAsTask (computation, taskCreationOptions = taskCreationOptions, cancellationToken = cancellationToken)
參數
computation
型別:Async<'T>要執行的計算。
taskCreationOptions
型別:TaskCreationOptions選擇性的工作建立選向。
cancellationToken
型別:CancellationToken選擇性的取消語彙基元。
傳回值
Task<TResult>物件,代表指定的運算。
範例
下列程式碼範例會示範 Async.StartAsTask 的用法。
open System.Windows.Forms
let bufferData = Array.zeroCreate<byte> 100000000
let async1 =
async {
use outputFile = System.IO.File.Create("longoutput.dat")
do! outputFile.AsyncWrite(bufferData)
}
let form = new Form(Text = "Test Form")
let button = new Button(Text = "Start")
form.Controls.Add(button)
button.Click.Add(fun args -> let task = Async.StartAsTask(async1)
printfn "Do some other work..."
task.Wait()
printfn "done")
Application.Run(form)
平台
Windows 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2
版本資訊
F# 核心程式庫版本
支援版本:4.0,可攜式執行檔 (PE)。