CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2002
    Posts
    71

    How to implement a delay???

    Hello, can someone tell me how to implement a wait function for a particular period of time (e.g., 5 secs)?

    In the program I'm writing, I'm controlling a piece of equipment that has a reset command which must be followed by a run command after the equipment has rebooted. Consequently, I must do the following:

    {
    //Reset the module
    api_reset_module();

    //Need to wait for 5 secs
    for (i=0;i<20000;i++)
    dummyvariable++;

    //Now restart the module
    api_restart_module();

    }

    Currently, I just have a for loop stuck in there at a loop count that effectively gets me ~> 5 sec delay but this is not very elegant I know!

    Can someone tell me if there is a way to implement a specific delay in a more elegant way?

    Thanks

    The Newb

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    There are several options. You can use 'Sleep(5000)' which will stoip execution of this thread for 5 seconds. The downside of doing this is for example if you have a GUI application your message queue will also not be processed during this time. So in other words there is no redrawing or other action (e.g. pressing a button).

    Another possibility is using other timers like waitable timers. The following is a sample how to use waitable timers. They will delay execution by 5 seconds in the following...
    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    int main()
    {
      HANDLE hTimer = NULL;
      LARGE_INTEGER liTime;
    
      liTime.QuadPart=-50000000;
    
      // Create a waitable timer.
      hTimer = ::CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
      if(!hTimer)
      {
        std::cout << "Could not create waitable timer (Error "
                  << ::GetLastError() << ")" << std::endl;
        ::Sleep(5000);
        return -1;
      }
    
      std::cout << "Wait for 10 seconds..." << std::endl;
    
      // Set timer
      if(!::SetWaitableTimer(hTimer, &liTime, 0, NULL, NULL, 0))
      {
        std::cout << "Could not set waitable timer (Error "
                  << ::GetLastError() << std::endl;
        ::Sleep(5000);
        return -1;
      }
    
      // Wait for timer
      if(::WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
        std::cout << "Could not wait for timer object (Error "
                  << ::GetLastError() << std::endl;
      else
        std::cout << "Time elapsed" << std::endl;
    
      // Wait for keystroke
      _getch();
    
      return 0;
    }
    For additional information you might take a look at http://www.codeproject.com/system/timers_intro.asp. It is a very comprehensive timer tutorial.

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Sleep() works great if you are using Windows. If you are using an OS with POSIX support, then use nanosleep(). You can usually find an example in the man pages.

    - Kevin

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    These guys made only correct statements. The only thing that I
    want to add is that you have to look in the OS documentation.
    This sort of thing is almost always going to be platform-specific
    because of the different hardware involved. If you're in neither
    windows nor a unix based OS [like if this is an embedded systems
    program], you'll have to turn to the OS manual then.

    Chances are, though, that Sleep(), sleep(), or nanosleep will help
    you just fine.

    --Paul

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by KevinHall
    Sleep() works great if you are using Windows. If you are using an OS with POSIX support, then use nanosleep(). You can usually find an example in the man pages.

    - Kevin
    Originally posted by PaulWendt
    These guys made only correct statements. The only thing that I
    want to add is that you have to look in the OS documentation.
    This sort of thing is almost always going to be platform-specific
    because of the different hardware involved. If you're in neither
    windows nor a unix based OS [like if this is an embedded systems
    program], you'll have to turn to the OS manual then.

    Chances are, though, that Sleep(), sleep(), or nanosleep will help
    you just fine.

    --Paul
    Ooopppsss....I somehow missed that this was posted in the Non-Visual-C++-Forum...thank you both for paying attention...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured