共用方式為


Async.AwaitIAsyncResult 方法 (F#)

更新:2010 年 8 月

建立將會等候 的非同步計算 IAsyncResult

命名空間/模組路徑: Microsoft.FSharp.Control

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
static member AwaitIAsyncResult : IAsyncResult * ?int -> Async<bool>

// Usage:
Async.AwaitIAsyncResult (iar)
Async.AwaitIAsyncResult (iar, millisecondsTimeout = millisecondsTimeout)

參數

  • iar
    型別:IAsyncResult

    要等待的 IAsyncResult。

  • millisecondsTimeout
    型別:int

    逾時值 (以毫秒為單位)。 如果未提供值,則會使用對應至 Infinite 的預設值 -1。

傳回值

會等待指定之 IAsyncResult 的非同步計算。

備註

計算傳回 true如果控點指出結果,以在 指定的逾時。

範例

下列程式碼範例說明如何使用 Async.AwaitIAsyncResult設定及執行時,會產生先前的.NET Framework 非同步作業觸發的計算的 中 IAsyncResult完成。 在此情況下呼叫 AwaitIAsyncResult會造成作業来等到進行檔案寫入作業完成之前開啟檔案進行 讀取。

open System.IO

let streamWriter1 = File.CreateText("test1.txt")
let count = 10000000
let buffer = Array.init count (fun index -> byte (index % 256)) 

printfn "Writing to file test1.txt."
let asyncResult = streamWriter1.BaseStream.BeginWrite(buffer, 0, count, null, null)

// Read a file, but use AwaitIAsyncResult to wait for the write operation
// to be completed before reading.
let readFile filename asyncResult count = 
    async {
        let! returnValue = Async.AwaitIAsyncResult(asyncResult)
        printfn "Reading from file test1.txt."
        // Close the file.
        streamWriter1.Close()
        // Now open the same file for reading.
        let streamReader1 = File.OpenText(filename)
        let! newBuffer = streamReader1.BaseStream.AsyncRead(count)
        return newBuffer
    }

let bufferResult = readFile "test1.txt" asyncResult count
                   |> Async.RunSynchronously

平台

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#)

IAsyncResult

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。