Async.AwaitWaitHandle Method (F#)

Creates an asynchronous computation that will wait for the supplied WaitHandle.

Namespace/Module Path: Microsoft.FSharp.Control

Assembly: FSharp.Core (in FSharp.Core.dll)

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

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

Parameters

  • waitHandle
    Type: WaitHandle

    The wait handle that can be signaled.

  • millisecondsTimeout
    Type: int

    The timeout value in milliseconds. If no timeout value is provided, the default value is -1, which corresponds to ystem.Threading.Timeout.Infinite.

Return Value

An asynchronous computation that waits on the given WaitHandle object.

Remarks

The computation returns true if the handle indicated a result within the given timeout.

Example

The following code example illustrates how to use Async.AwaitWaitHandle to set up a computation to run when another asynchronous operation is completed, as indicated by a wait handle.

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

Writing to file BigFile.dat.
Reading from file BigFile.dat.

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Control.Async Class (F#)

Microsoft.FSharp.Control Namespace (F#)