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.


Download demo project - 3 Kb


  • Great!!!!

    Posted by Billy Boy on 09/15/2016 06:12am

    You are god! Great job!

  • Great!

    Posted by Oliver on 06/15/2016 07:38pm

    This is exactly what I was looking for, previously I had a problem where when I clicked out of my program it would crash because of the Sleep() function. XSleep works amazingly!

  • 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

  • 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

  • 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...

  • 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:
    #ifdef __AFX_H__
    while(::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    dwTimeout = dwStopTime - GetTickCount();
    if(!dwTimeout || dwTimeout>dwWaitInMSecs)

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

Top White Papers and Webcasts

  • Essential Insights for Successful Cloud Migration and Management Whether you're planning your cloud migration strategy or already in the cloud, making accurate cloud decisions requires a deep analytical approach. This paper discusses the main objectives to achieve, top questions to ask, and the analytics you need at each stage of your cloud journey. You'll learn: How to identify which cloud provider will provide the best cost and performance benefits for your organization How to determine which applications …

  • Featuring Art Schoeller, VP and Principal Analyst, at Forrester Research Live Event Date: November 9, 2016 @ 10 AM PT / 1 PM ET Being "proactive" with your customers is not enough to survive in the market today. The truth is, organizations that understand how to engage customers on their preferred channels will create contextual, and relevant experiences for customers. Plus, they will see the financial impact of nurturing long-term customer loyalty. Join our guest speaker Art Schoeller, VP and Principal …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date