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: WaitHandleHandle di attesa che può essere segnalato.
millisecondsTimeout
Tipo: intValore 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 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2.
Informazioni sulla versione
F# Runtime
Supportato in: 2.0, 4.0
Silverlight
Supportato in: 3
Vedere anche
Riferimenti
Spazio dei nomi Microsoft.FSharp.Control (F#)
Cronologia delle modifiche
Data |
Cronologia |
Motivo |
---|---|---|
Agosto 2010 |
Aggiunto esempio di codice. |
Miglioramento delle informazioni. |