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

Async.AwaitIAsyncResult Method (F#)

Den Delimarsky|Last Updated: 6/9/2016
|
1 Contributor

Creates an asynchronous computation that will wait on the System.IAsyncResult.

Namespace/Module Path: Microsoft.FSharp.Control

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

Syntax

// Signature:
static member AwaitIAsyncResult : IAsyncResult * ?int -> Async<bool>

// Usage:
Async.AwaitIAsyncResult (iar)
Async.AwaitIAsyncResult (iar, millisecondsTimeout = millisecondsTimeout)

Parameters

iar Type: System.IAsyncResult

The IAsyncResult to wait on.

millisecondsTimeout Type: int

The timeout value in milliseconds. If one is not provided then the default value of -1 corresponding to System.Threading.Timeout.Infinite.

Return Value

Returns an asynchronous computation that waits on the given System.IAsyncResult.

Remarks

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

The following code example illustrates how to use Async.AwaitIAsyncResult to set up and execute a computation that is triggered when a previous .NET Framework asynchronous operation that produces an System.IAsyncResult finishes. In this case, the call to AwaitIAsyncResult causes the operation to wait for a file write operation to be completed before opening the file for reading.

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

Platforms

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#)

© 2016 Microsoft