While Loops

Category:
For and While Loops
Description: A simple 'while' loops that busy-waits until the given time-span has passed
Code:
let SampleWhileLoop2() =
let start = DateTime.Now
let duration = System.TimeSpan.FromMilliseconds(8.0)
let diff (a:DateTime) (b:DateTime) = System.DateTime.op_Subtraction(System.DateTime.Now,b)
printfn "Waiting..."

// Here's the loop
while diff DateTime.Now start < duration do
printfn "."

// OK, we're done...
let span = diff DateTime.Now start
printfn "\nAttempted to busy-wait 8ms, actually waited %dms" span.Milliseconds

//--------------------------------------------------------

Execution Result:
Waiting...
.
.
.
.
.
.
.

Attempted to busy-wait 8ms, actually waited 8ms

Last edited Sep 14, 2011 at 2:36 AM by ttliu2000, version 1

Comments

No comments yet.