Thread: Problem with Program not Quitting

  1. #1
    Unregistered
    Guest

    Question Problem with Program not Quitting

    Hi, I'm new to windows programming and am having a problem with my program not quitting properly. When I close it, the program disappears from view and the taskbar, and it looks like it's gone, but when I ctrl-alt-del it's still running, and it won't let me re-build it until I've closed it. Here are the parts of my code that I guess would be causing the problem:


    ---
    switch(message) {
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    ---

    in WndProc()

    and

    ---

    bool done = false;

    while(!done)
    {
    if(PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE))
    {
    if(msg.message == WM_QUIT)
    done = true;

    else
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    }

    return msg.wParam;

    ---

    in WinMain().

    If there's any other area of the program you need to see, just say so and I'll post it.

    Thanks!

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Add

    DestroyWindow(hWnd);// after the PostQuitMessage()
    "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

  3. #3
    Unregistered
    Guest
    Hmm, I added that and it's still not working. Do I need a case WM_CLOSE too?

    Thanks!

  4. #4
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    From the SDK:
    PM_REMOVE Messages are removed from the queue after processing by PeekMessage.
    So maybe your if statement is never true....

    Maybe try PM_NOREMOVE?

    Just a guess. Let me know if it works.

    Edit:

    I don't know what other cases you have in your Callback function, but the above might explain why PostQuitMessage is never called as well.
    Last edited by Invincible; 06-05-2002 at 04:43 AM.
    "The mind, like a parachute, only functions when open."

  5. #5
    Unregistered
    Guest
    I tried putting PM_NOREMOVE, and that causes my program to freeze as soon as it starts. I put a MessageBox after the if(message == WM_QUIT) and it's never true. I guess the problem's in my Callback function; here it is:

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;

    switch(message)
    {
    case WM_DESTROY:
    case WM_CLOSE:
    PostQuitMessage(0);
    DestroyWindow(hwnd);
    break;

    case WM_SIZE:
    ...
    break;

    case WM_PAINT:
    ...
    break;

    case WM_LBUTTONDOWN:
    ...
    break;

    case WM_RBUTTONDOWN:
    ...
    break;

    case WM_RBUTTONUP:
    ...
    break;

    case WM_MOUSEMOVE:
    ...
    break;

    case WM_KEYDOWN:
    switch(wParam)
    {
    ...
    }

    default:
    return DefWindowProc(hwnd, message, wParam, lParam);
    }
    }

    Thanks!

  6. #6
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Angry

    It has something to do with the way we're trying to use PeekMessage. I tried using using PeekMessage and ran into the same problems.

    I tried lots of things trying to get it to work, but I had no luck.
    I even found a tutorial on how to use it, specifically:

    http://sunlightd.virtualave.net/Wind...irstSteps.html

    Still, I had the same problems.

    I'll try again when I have some time because this my battle now too.
    "The mind, like a parachute, only functions when open."

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try setting the 2nd param of PeekMessage to NULL instead of the specific wnd handle.

    ie PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)

  8. #8
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Tried it already, no go.
    "The mind, like a parachute, only functions when open."

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Sorry, stuffed up, try

    case WM_CLOSE:
    DestroyWindow();//will post the WM_DESTROY msg
    break;
    case WM_DESTROY:
    PostQuitMessage(0);//will put the WM_QUIT
    break;

    Then use SendMessage() to test the WM_CLOSE is entered (and works).


    Try using GetMessage() not PeekMessage() unless you have a specific reason for the PeekMessage().
    "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

  10. #10
    Unregistered
    Guest
    Ok, I made all of those changes, and it still does the same thing. I added PostMessages and both the WM_CLOSE and WM_DESTROY ones worked, but the message after the if(msg.message == WM_QUIT) didn't.

  11. #11
    Unregistered
    Guest
    Oops, I meant I added MessageBoxes, not PostMessages.

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Is the missing break after the WM_KEYDOWN a typo?

    Try removing the WM_DESTROY case compleatly and only have the WM_CLOSE. Try a return FALSE; (instead of a break)

    Why are you using PeekMessage() and not GetMessage()?
    Is it a game or time critical app?
    "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

  13. #13
    Unregistered
    Guest
    Hmm, I tried all your suggestions and same problem.

    Yeah, it's going to be a game once I get this part working. Anyway, I tried the GetMessage and it didn't work either.

  14. #14
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    You're not returning after PostQuitMessage(0). You should be returning 0 afterwards to let it know you've handled the message.

    Code:
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return(0);
    }break;

  15. #15
    Unregistered
    Guest
    I tried it and same problem

    Thanks though...

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