Table of contents
Collapse the table of content
Expand the table of content

Async.AwaitWaitHandle Method (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

Creates an asynchronous computation that will wait for the supplied System.Threading.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)


waitHandle Type: System.Threading.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 System.Threading.Timeout.Infinite.

Return Value

Returns an asynchronous computation that waits on the given System.Threading.WaitHandle object.


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


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.
        // 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


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


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Control.Async Class (F#)

Microsoft.FSharp.Control Namespace (F#)

© 2019 Microsoft