Message loop question.

This is a discussion on Message loop question. within the Windows Programming forums, part of the Platform Specific Boards category; As far as I know the GetMessage() function returns zero if it receives a WM_QUIT message. This is often used ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Message loop question.

    As far as I know the GetMessage() function returns zero if it receives a WM_QUIT message. This is often used as a way to stop the message loop and terminate the program. Since I like to play around with my code to see the actual effects of API functions and how they could be used I tried to remove my PostQuitMessage() function just to see if the program remains in memory after the main window is destroyed. I noticed it quits completely, and I want to know why just for the sake of knowing
    I really appreciate all help I can get.

    Here is my message loop
    Code:
    while(GetMessage(&msg, WndPtr, 0, 0) > 0)
    {
    	TranslateMessage(&msg);
    	DispatchMessage(&msg);
    }
    and here's my WndProc
    Code:
    LRESULT CALLBACK msg_control(HWND WndPtr, unsigned int msg, WPARAM wParam, LPARAM lParam)
    {
    	if (msg == WM_CLOSE)
    	{
    		DestroyWindow(WndPtr);
    	}
    	else if(msg == WM_DESTROY)
    	{
    		//PostQuitMessage(0);
    	}
    	else
    	{
    		return DefWindowProc(WndPtr, msg, wParam, lParam);
    	}
    	return 0;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your window might have vanished from sight and been destroyed but your message loop is still spinning contentedly away - ie., your program is still running. To confirm this, check it with the task manager.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    The thing is, I have checked the taskmanager and it confirms that the entire process is terminated when I destroy the window. I can't understand why the loop quits.
    Last edited by antex; 04-03-2007 at 05:42 AM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    When window is destroyed GetMessage called with its hwnd as a param returns -1 indicating error.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    while(GetMessage(&msg, WndPtr, 0, 0) > 0)
    A message loop should use NULL as the second argument to GetMessage. As vart pointed out, your message loop relies on an error to exit. The WM_QUIT will never be received as it is sent to the thread and not a specific window.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Doh!

    One of these days I might just actually start looking at the code before leaping to an erroneous conclusion.

    Sorry, antex - fortunately both vart and 'tmouse were paying attention to your supplied code; please ignore my earlier assertion which did not take into account the code you were actually using.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ah okey, thank you all very much for replying, this saved me from (more) confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Pausing in the message loop
    By IdioticCreation in forum Game Programming
    Replies: 7
    Last Post: 01-22-2007, 06:21 PM
  3. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 09:07 AM
  4. question about for loop
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 09-27-2005, 05:59 AM
  5. Message loop not working...
    By Hunter2 in forum Windows Programming
    Replies: 24
    Last Post: 07-03-2003, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21