Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Async.Sleep Method (F#)

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)

millisecondsDueTime

Type: int

The number of milliseconds to sleep.

Exception

Condition

ArgumentOutOfRangeException

Thrown when the due time is negative and not infinite.

An asynchronous computation that will sleep for the given time.

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

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# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Community Additions

ADD
Show:
© 2015 Microsoft