Thread: Problem with Program not Quitting

  1. #16
    Unregistered
    Guest
    I don't think your message loop is correct. Try this:
    Code:
    while(GetMessage(&msg, NULL, 0, 0))
    {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
    }
    return msg.wParam;
    I believe WM_QUIT will make GetMessage() return 0, which will then won't satisfy the condition in the while loop, so it should skip down to the return statement and end your program.

    Alternatively, to allow for the error checking in GetMessage() (it returns -1 if there is any error), you could try something like this for your message loop:
    Code:
    int i;
    while(1)
    {
         i = GetMessage(&msg, NULL, 0, 0);
         if(i == -1) { /* Pop up a message box or something, there has been an error. */ }
         else if(i == 0) { break; /* WM_QUIT has been received, break the loop. */ }
         else
         {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
         }
    }
    return msg.wParam;

  2. #17
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    What version of Windows are you running?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #18
    Unregistered
    Guest
    Wow, thanks! It works. One more question though: again, I'm new to windows programming, so isn't GetMessage a lot slower than PeekMessage? I'm trying to make a game.

    I tried using PeekMessage instead in your example, and my program froze with the hourglass as soon as it was executed.

    Oh, and I'm using WinXP Pro.

    Thanks!

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try removing the 'else' from the message loop.

    ie
    if(msg.message==WM_QUIT) bDone=TRUE;
    TranslateMessage();
    DispatchMessage();
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I personally think you should use while(PeekMessage()) instead of while(GetMessage()) if you want the program to keep doing things while there are no messages... otherwise GetMessage() will block the program from running until you move the mouse or type a key. I also had a problem with my program freezing... but try adding "Sleep(1);" at the end of your while(!done) loop. That fixed mine for some odd reason. Or, if you don't want your program to go slow and then fast and slow and fast, you can do this:

    Code:
    #define WAIT_MILLISECS 20
    
    while(!done)
    {
         long oldTime = GetTickCount();
    
         while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //or whatever parameters it needs
         {
              (...)
         }
         
         if(WAIT_MILLISECS - (GetTickCount() - oldTime) > 1)
              Sleep(WAIT_MILLISECS - (GetTickCount() - oldTime));
         else
              Sleep(1);
    }
    
    that's what I'm using.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm not sure if you just didn't post your code right but in your callback function you should have some sort of return. What I mean is that the only return value is the default case. Try putting the DefWndProc() call outside of the switch statment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM