XSleep - An alternative to the Sleep() function | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 14, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.