www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to properly Thread.sleep?

reply "Israel" <tl12000 live.com> writes:
Ive tried using google but no matter what code i find it either 
doesnt work, compiler gives me errors or maybe the code is 
deprecated.

What is the proper way of adding sleep time to a program?
Ive tried..

import std.stdio;
import core.thread;

void main()
{
      writeln("Sleep..");
      sleep(200);
      writeln("done");
}

but all i get is a compiler error unidentified identifier sleep.

The documentation examples dont make sense. Why would it work 
this way?

Thread.sleep( dur!("msecs")( 50 ) );  // sleep for 50 milliseconds
Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds
Mar 25 2015
next sibling parent reply "Sad panda" <asdf asdf.com> writes:
On Wednesday, 25 March 2015 at 17:23:40 UTC, Israel wrote:
 Ive tried using google but no matter what code i find it either 
 doesnt work, compiler gives me errors or maybe the code is 
 deprecated.

 What is the proper way of adding sleep time to a program?
 Ive tried..

 import std.stdio;
 import core.thread;

 void main()
 {
      writeln("Sleep..");
      sleep(200);
      writeln("done");
 }

 but all i get is a compiler error unidentified identifier sleep.

 The documentation examples dont make sense. Why would it work 
 this way?

 Thread.sleep( dur!("msecs")( 50 ) );  // sleep for 50 
 milliseconds
 Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds
Thread.sleep(200.msecs); Thread.sleep(12.seconds); Thread.sleep(1.minutes);
Mar 25 2015
parent "Israel" <tl12000 live.com> writes:
On Wednesday, 25 March 2015 at 17:25:50 UTC, Sad panda wrote:
 Thread.sleep(200.msecs);
 Thread.sleep(12.seconds);
 Thread.sleep(1.minutes);
There we go, thank you so much.
Mar 25 2015
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 25 March 2015 at 17:23:40 UTC, Israel wrote:
 What is the proper way of adding sleep time to a program?
The documentation examples are exactly how you can do it: Thread.sleep( dur!("msecs")( 50 ) ); // sleep for 50 milliseconds Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds Try one of those lines in your program and you'll see it work. You can also call the various C functions to do it but the Thread.sleep is often a bit easier.
 The documentation examples dont make sense. Why would it work 
 this way?
sleep is a static method on the Thread class, so you need to call it Thread.sleep instead of plain sleep, and the argument uses a different type to make the units a bit more clear. I believe Thread.sleep(3.seconds); shoudl also work.
Mar 25 2015
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/25/2015 10:23 AM, Israel wrote:

 Ive tried using google but no matter what code i find it either doesnt
 work, compiler gives me errors or maybe the code is deprecated.

 What is the proper way of adding sleep time to a program?
 Ive tried..

 import std.stdio;
 import core.thread;

 void main()
 {
       writeln("Sleep..");
       sleep(200);
Replace that with Thread.sleep(200.msecs);
       writeln("done");
 }

 but all i get is a compiler error unidentified identifier sleep.

 The documentation examples dont make sense.
Here is alternative documentation: http://ddili.org/ders/d.en/parallelism.html#ix_parallelism.Thread.sleep
 hy would it work this way?

 Thread.sleep( dur!("msecs")( 50 ) );  // sleep for 50 milliseconds
 Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds
UFCS makes it easier by making those 50.msecs and 5.seconds. Ali
Mar 25 2015