Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:10:48+00:00 2026-06-05T23:10:48+00:00

Does a Sleep(sometime) serve a purpose in the typical infinite window message loop, or

  • 0

Does a Sleep(sometime) serve a purpose in the typical infinite window message loop, or is it just useless or even harmful?

Some examples contain the Sleep, most of them do not.

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        Sleep(500); // As pointed out below, completely nonsense
        Sleep(5); // Would have been the better example, but still bad
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:10:49+00:00Added an answer on June 5, 2026 at 11:10 pm

    That call is pointless. GetMessage waits until there’s a message in the queue,and while it does, your program will not be taking up CPU. There’s no need to try to do what it already does.

    Now about being harmful, maybe (very likely) it will! If there are 1000 messages in the queue, it’s going to sleep for 500 seconds before it can process them all. In that time, you’ll have accumulated much more than 1000 messages. It won’t be long before your window becomes completely useless. Windows do get a lot of messages. You’re going to tell me that you’ll wait half a second to respond each time the mouse moves at all over your window?

    Also, from the documentation, GetMessage will return -1 if there’s an error. Since -1 is not 0, your loop will try to process the message anyway. The more correct way would be to either put in a handler, or exit altogether:

    while (GetMessage (&msg, NULL, 0, 0) > 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just curious. How does actually the function Sleep() work (declared in windows.h)? Maybe not
When does Java's Thread.sleep throw InterruptedException ? Is it safe to ignore it? I
Does anyone know a Library which provides a Thread.sleep() for Java which has an
sleep() is a static method of class Thread. How does it work when called
I have two questions concerning the sleep() function in PHP: Does the sleep time
I have a java program that does some calculations and then uploads the results
Does Android wake from deep sleep in order to broadcast ACTION_TIME_TICK ? From experimenting,
Possible Duplicate: When does Java's Thread.sleep throw InterruptedException? I saw we have to catch
I have a thread that does several things. One of them is to just
Im trying to create a NSThread game loop, i have some of the time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.