更新:2010 年 7 月
建立會在指定的時間內休眠的非同步計算。 這被排定使用一個 Timer物件。 在等待期間,作業將不會封鎖作業系統執行緒。
命名空間/模組路徑:Microsoft.FSharp.Control
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
static member Sleep : int -> Async<unit>
// Usage:
Async.Sleep (millisecondsDueTime)
參數
millisecondsDueTime
型別:int要睡眠的毫秒數。
例外狀況
例外狀況 |
條件 |
|---|---|
在到期時間是負的且不是無限時擲回。 |
傳回值
會睡眠所指定時間的非同步計算。
範例
下列程式碼範例示範如何使用 Async.Sleep來模擬的運算所執行的 特定工期。
let simulatedJob id time =
let timestamp() = System.DateTime.Now.Ticks
async {
printfn "Job %d start" id
let timestamp1 = timestamp()
do! Async.Sleep(time * 1000)
let timestamp2 = timestamp()
let timespan = System.TimeSpan(timestamp2 - timestamp1)
printfn "Job %d end %s" id (timespan.ToString("G"))
}
[ 1 .. 10]
|> List.mapi (fun index time -> simulatedJob index time)
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
範例輸出
交錯輸出,因為有多個執行緒同時執行。
平台
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
請參閱
參考
Microsoft.FSharp.Control 命名空間 (F#)
變更記錄
日期 |
History |
原因 |
|---|---|---|
|
2010 年 7 月 |
加入程式碼範例。 |
資訊加強。 |