Async.AwaitIAsyncResult メソッド (F#)

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 を使用して、IAsyncResult を生成する .NET Framework の前の非同期操作が終了したときにトリガーされる計算を設定および実行する方法を次のコード例に示します。この場合、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 8、Windows 7、Windows Server 2012 で Windows Server 2008 R2

バージョン情報

F# コア ライブラリのバージョン

サポート: ポータブル 2.0、4.0

参照

関連項目

Control.Async クラス (F#)

Microsoft.FSharp.Control 名前空間 (F#)

IAsyncResult