Thread: sleep

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    sleep

    hi all
    how can i sleep a command that is popen("xxxxxx","r");
    to two seconds.
    i tried like this
    sleep(2)
    popen("xxxxxx","r");

    but it is not waiting for 2 seconds. it is directly excuting.
    is there any command other than sleep() to wait for 2 seconds.

    please help me

    thank you in advance

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Sleep is in milliseconds. For 2 seconds, you'd have Sleep(2000) instead. Using Sleep for much longer than than a few dozen milliseconds isn't really recommended because it can delay the closing of a program. You could try using a timer instead of Sleep.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by ulillillia View Post
    Sleep is in milliseconds. For 2 seconds, you'd have Sleep(2000) instead. Using Sleep for much longer than than a few dozen milliseconds isn't really recommended because it can delay the closing of a program. You could try using a timer instead of Sleep.
    yeah i tried but it saying,
    undefined reference to `Sleep'
    can u help me

    thank you in advance

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Sleep is Windows function. Include Windows.h to use it. Also note that it is "Sleep" with the capital "S" as the first letter.

    What IDE/compiler do you use?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by ulillillia View Post
    Sleep is Windows function. Include Windows.h to use it. Also note that it is "Sleep" with the capital "S" as the first letter.

    What IDE/compiler do you use?
    yeah!
    can use it in unix plz.

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Unfortunately, I don't know anything about Unix (never even seen it either) so I can't help you there. Short of using a basic timer system, such as the clock() function, I don't have any other alternatives. For proper use, you'd set a starting time and adjust the current time in a loop and until the difference is what you want, then continue on. Here's some pseudo code to give the general idea:

    Code:
    long StartTime, CurrentTime, EndTime;
    
    StartTime = clock(); // set the starting time
    CurrentTime = StartTime;
    EndTime = StartTime + 2000; // 2000 milliseconds or 2 seconds
    
    while (CurrentTime < EndTime) // loop until the ending time is reached
    {
    	CurrentTime=clock();
    }
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Uhm. How about sleeping AFTER the call to popen()?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Why do you think you need to sleep after calling popen() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by ulillillia View Post
    Unfortunately, I don't know anything about Unix (never even seen it either) so I can't help you there. Short of using a basic timer system, such as the clock() function, I don't have any other alternatives. For proper use, you'd set a starting time and adjust the current time in a loop and until the difference is what you want, then continue on. Here's some pseudo code to give the general idea:

    Code:
    long StartTime, CurrentTime, EndTime;
    
    StartTime = clock(); // set the starting time
    CurrentTime = StartTime;
    EndTime = StartTime + 2000; // 2000 milliseconds or 2 seconds
    
    while (CurrentTime < EndTime) // loop until the ending time is reached
    {
    	CurrentTime=clock();
    }
    how can i use this to wait 2 seconds in my program.
    can you please help me
    thank you in advance

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    According to my compiler help files:

    C++ -> Sleep(2000);
    C -> sleep(2000);

    The capital makes a diiference
    Double Helix STL

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by swgh View Post
    According to my compiler help files:

    C++ -> Sleep(2000);
    C -> sleep(2000);

    The capital makes a diiference
    i am sorry to say it is not working.
    i am using this sleep() in some other function (not in main())
    is this any problem?

    help me.
    thank you in advacne

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    This waits for two seconds:

    Code:
    #include <stdio.h>
    #include <windows.h> /*to define sleep()*/
    
    int main ( void )
    {
       sleep(2000);  /*wait 2 seconds*/
       printf("Now you can see this\n");
    
       getchar();
    
       return 0;
    }
    Double Helix STL

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by swgh View Post
    This waits for two seconds:

    Code:
    #include <stdio.h>
    #include <windows.h> /*to define sleep()*/
    
    int main ( void )
    {
       sleep(2000);  /*wait 2 seconds*/
       printf("Now you can see this\n");
    
       getchar();
    
       return 0;
    }
    in my program the sleep() is not belongs to main().
    if i use sleep in main it is working fine but other than main it is not.

    help me

    thank you in advance

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    As longs you #include <windows.h> in your program before you use sleep() it should work. Does your compiler have the windows.h library? What compiler are you using, as it is OS an compiler specific
    Double Helix STL

  15. #15
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by swgh View Post
    As longs you #include <windows.h> in your program before you use sleep() it should work. Does your compiler have the windows.h library? What compiler are you using, as it is OS an compiler specific
    am doing my program in unix, i think this the problem.

    thank you in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 02:30 AM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM