Async.AwaitIAsyncResult Method (F#)
Creates an asynchronous computation that will wait on the IAsyncResult.
Namespace/Module Path: Microsoft.FSharp.Control
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
static member AwaitIAsyncResult : IAsyncResult * ?int -> Async<bool>
// Usage:
Async.AwaitIAsyncResult (iar)
Async.AwaitIAsyncResult (iar, millisecondsTimeout = millisecondsTimeout)
Parameters
iar
Type: IAsyncResultThe IAsyncResult to wait on.
millisecondsTimeout
Type: intThe timeout value in milliseconds. If one is not provided then the default value of -1 corresponding to Infinite.
Return Value
An asynchronous computation that waits on the given IAsyncResult.
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.AwaitIAsyncResult to set up and execute a computation that is triggered when a previous .NET Framework asynchronous operation that produces an 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 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