Async.Sleep Method (F#)
Creates an asynchronous computation that will sleep for the given time. This is scheduled using a Timer object. The operation will not block operating system threads for the duration of the wait.
Namespace/Module Path: Microsoft.FSharp.Control
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
static member Sleep : int -> Async<unit>
// Usage:
Async.Sleep (millisecondsDueTime)
Parameters
millisecondsDueTime
Type: intThe number of milliseconds to sleep.
Exceptions
Exception |
Condition |
---|---|
Thrown when the due time is negative and not infinite. |
Return Value
An asynchronous computation that will sleep for the given time.
Example
The following code example shows how to use Async.Sleep to simulate computations that run for specific durations.
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
Sample Output
The output is interleaved, because there are multiple threads running at the same time.
Job Job 0 start 1 start Job 2 starJob 3 start Job 4 start Job 5 start Job 6 start Job 7 start Job 8 start Job 9 start t Job 0 end 0:00:00:01.0091009 Job 1 end 0:00:00:02.0102010 Job 2 end 0:00:00:03.0033003 Job 3 end 0:00:00:04.0074007 Job 4 end 0:00:00:05.0065006 Job 5 end 0:00:00:06.0076007 Job 6 end 0:00:00:07.0007000 Job 7 end 0:00:00:07.9957995 Job 8 end 0:00:00:08.9928992 Job 9 end 0:00:00:09.9919991
Platforms
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
Version Information
F# Core Library Versions
Supported in: 2.0, 4.0, Portable