共用方式為


Async.AwaitWaitHandle 方法 (F#)

更新:2010 年 8 月

建立將等待提供 的非同步計算 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.Infinite。

傳回值

非同步的計算,會等候指定的 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

輸出

      

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Control.Async 類別 (F#)

Microsoft.FSharp.Control 命名空間 (F#)

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。