Async.AwaitWaitHandle 方法 (F#)
建立會等待所提供之 WaitHandle 的非同步計算。
**命名空間/模組路徑:**Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member AwaitWaitHandle : WaitHandle * ?int -> Async<bool>
// Usage:
Async.AwaitWaitHandle (waitHandle)
Async.AwaitWaitHandle (waitHandle, millisecondsTimeout = millisecondsTimeout)
參數
waitHandle
型別:WaitHandle可以接收信號的等待控制代碼。
millisecondsTimeout
型別:int逾時值 (以毫秒為單位)。如果未提供逾時值,則預設值為-1,對應於 ystem.Threading.Timeout.Infinit。
傳回值
會等待指定之 WaitHandle 物件的非同步計算。
備註
如果控制代碼在指定的逾時內顯示結果,則計算作業會傳回 true。
範例
下列程式碼範例說明如何使用 Async.AwaitWaitHandle 設定要在另一個非同步作業完成時 (如等待控制代碼所示) 執行的計算。
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
平台
Windows 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2
版本資訊
F# 核心程式庫版本
支援版本:2.0, 4.0,可攜式執行檔 (PE)。