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

修订记录

Date

修订记录

原因

2010 年 8 月

添加了代码示例。

信息补充。