XSleep - An alternative to the Sleep() function

Environment: Windows 9x/NT, Visual C++ 6.0

If you use Sleep() in your code, you will soon realize that your application "appears" to block. This is because the Sleep() function does not process the message pump, and due to this your application gives the appearance of "hanging" for some time.

I've written an alternative function which keeps processing the message pump to ensure that all message are posted even while the Sleep is in progress.

You may not find it neccessary to use the XSleep() function all the time. For instance, in a console based application. There are some scenarios where you may need this over Sleep(), especially if you're using Sleep in a GUI application.

The XSleep() code uses only Win32 calls, and so it can be used in both MFC and Win32 applications.

To use XSleep() include the XSleep.h file into your project and then call the XSleep() function with the required sleep duration in miliseconds as the parameter.


XSleep(1000); // sleep for 1 second

A demo Visual C++ 6.0 project is available for download. The demo creates two threads and makes them both go to sleep for 2 seconds.

Downloads

Download demo project - 3 Kb


Comments

  • adwczx sj zX hEI itLd pq

    Posted by PiaRbNeAqh on 06/25/2013 02:01pm

    http://www.ami-bonnymethod.org/cgi/brandviagra/#19 does viagra for women work - viagra us online pharmacy

    Reply
  • Problem with handle count

    Posted by Bob20100 on 05/03/2005 06:24am

    I opened Task Manager and I noticed that the count of Handles increased every time I called XSleep. I solved the problem getting the handle returned by CreateHandle and executing CloseHandle() before leaving XSleep() function

    Reply
  • Ok but does not work in a dialog... eats messages and blows up

    Posted by Legacy on 05/08/2000 12:00am

    Originally posted by: Hans Wedemeyer

    Does not work in a dialog.
    Eats mouse messages and if called more than a few times blows away Windows NT...

    Reply
  • Another way

    Posted by Legacy on 01/15/2000 12:00am

    Originally posted by: John Bundgaard

    I have a suggestion for another way of doing this.
    
    I does not need to start a worker thread and will
    support MFC projects better.

    void XSleepAnotherWay(DWORD dwWaitInMSecs)
    {
    DWORD dwStopTime = GetTickCount() + dwWaitInMSecs;
    DWORD dwTimeout = dwWaitInMSecs;
    MSG msg;
    while(1) {
    switch(MsgWaitForMultipleObjects(0, NULL, FALSE, dwTimeout, QS_ALLINPUT)) {
    case WAIT_TIMEOUT:
    break;
    default:
    #ifdef __AFX_H__
    while(::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    AfxGetApp()->PumpMessage();
    #else
    while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
    }
    #endif
    }
    dwTimeout = dwStopTime - GetTickCount();
    if(!dwTimeout || dwTimeout>dwWaitInMSecs)
    break;
    }
    }


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Mobile devices, social business apps, and business analytics are converging with the Cloud to create the most substantial changes in technology since the Internet revolution. Businesses have to change the way they think and operate, and with rising budgets for technology, they need someone to provide the services that will keep them competitive in this environment. Learn more about the important technology trends you need to stay on top of to ensure your business doesn't get left behind.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds