Async.AwaitWaitHandle 方法 (F#)

创建将等待提供的 WaitHandle 的异步计算。

命名空间/模块路径: Microsoft.FSharp.Control

程序集:FSharp.Core(在 FSharp.Core.dll 中)

// Signature:
static member AwaitWaitHandle : WaitHandle * ?int -> Async<bool>

// Usage:
Async.AwaitWaitHandle (waitHandle)
Async.AwaitWaitHandle (waitHandle, millisecondsTimeout = millisecondsTimeout)

参数

  • waitHandle
    类型:WaitHandle

    可被终止的等待句柄。

  • millisecondsTimeout
    类型:int

    超时值(以毫秒为单位)。 如果未提供超时值,则使用默认值 -1,且与 ystem.Threading.Timeout.Infinite 相对应。

返回值

一个等待给定的 WaitHandle 对象的异步计算。

备注

如果该句柄在给定超时时间内指示一个结果,则计算将返回 true。

示例

以下代码示例演示了如何使用 Async.AwaitWaitHandle 设置在计算另一个异步操作完成时运行一个计算,如等待句柄所示。

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 the waitHandle to wait for the write operation
// to be completed before reading.
let readFile filename waitHandle count = 
    async {
        let! returnValue = Async.AwaitWaitHandle(waitHandle)
        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.AsyncWaitHandle count
                   |> Async.RunSynchronously

Output

  
  

平台

Windows 8,Windows 7,Windows server 2012中,Windows server 2008 R2

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Control.Async 类 (F#)

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