Metodo Async.AwaitWaitHandle (F#)

Consente di creare un calcolo asincrono che attende il WaitHandle fornito.

Percorso di spazio dei nomi/modulo: 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)

Parametri

  • waitHandle
    Tipo: WaitHandle

    Handle di attesa che può essere segnalato.

  • millisecondsTimeout
    Tipo: int

    Valore di timeout in millisecondi.Se per il timeout non viene fornito alcun valore, viene utilizzato un valore predefinito -1 che corrisponde a System.Threading.Timeout.Infinite.

Valore restituito

Calcolo asincrono in attesa dell'oggetto WaitHandle specificato.

Note

Il calcolo restituisce true se l'handle ha indicato un risultato nell'arco del timeout specificato.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare Async.AwaitWaitHandle per impostare un calcolo da eseguire quando un'altra operazione asincrona viene completata, come indicato da un handle di attesa.

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

  
  

Piattaforme

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

Informazioni sulla versione

Versioni della libreria di base F#

Supportato in: 2,0, 4,0, portabile

Vedere anche

Riferimenti

Classe Control.Async (F#)

Spazio dei nomi Microsoft.FSharp.Control (F#)