Thursday, November 17, 2011

Using sleep in dev cpp

Sometime we require a screen to stay for a while and moreover we don't want to use key pressing to move, we just want to use the concept of sleep for a while then its quite easy in dev we just need to write the code :


#include <time.h>
 
time_t now, later;
 
void sleep(int delay)
{
 now=time(NULL);
 later=now+delay;
 while(now<=later)now=time(NULL);
}
 

"TIME.H" comes pre installed with dev cpp. So need not worry about it.

Wherever you want ot use sleep now just call

sleep(2);
here 2 denotes the number of seconds it will sleep for. Keep in mind its seconds not milliseconds.
Now a screen can stay and move whenever you want without using keyboard.

3 comments:

  1. This is wait and not sleep :)

    wait consumes CPU cycles on behalf of the calling thread (something what your while loop is doing) where as sleep suspends the execution of the calling thread.

    The unistd.h library of C has a sleep method - http://pubs.opengroup.org/onlinepubs/009604599/functions/sleep.html

    This sleep implementation delegates the responsibility to the kernel using an osapi method which then suspends the thread putting it into a sleeping state.

    ReplyDelete
  2. In header file WINDOWS.H

    we have Sleep(t);

    here t is time in milliseconds.

    You can use any one.

    ReplyDelete