Hello everyone,
In this post it might not be as interesting. I created a "while" loop with the purpose of continual runtime of the application until it closes. However, I came to a conundrum. In this "while" loop-

01:    while(true)
02:    {
03:      if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
04:      {
05:        if(msg.message == WM_QUIT)
06:        {
07:          TranslateMessage(&msg);
08:          DispatchMessage(&msg);
09:          break;
10:        }
11:      }
12:      else
13:      {
14:        AppIterate();
15:        TranslateMessage(&msg);
16:        DispatchMessage(&msg);
17:      }
18:    }//Close while
19:    //return (int)msg.wParam;
20:    return (int)msg.wParam;
21:  }//Close primary if statement

It is located in the body of the WinMain. Now, here is the question that gives me a round about. Please take a look at lines 7 through 9. I placed a break after TranslateMessage and DispatchMessage. Before, I have always placed a break before the two functions until just recently. Now, if I were to place a break before translate and dispatch then compile like so -

05:        if(msg.message == WM_QUIT)
06:        {
07:          Break;
08:          TranslateMessage(&msg);
09:          DispatchMessage(&msg);
10:        }

If the message was true and breaks out of the while loop before going through the translate and dispatch functions, wouldn't the application continue to run even though it is actually closed?(Hence it goes into and infinite loop that would not quit until the memory is depleted and goes into memory dump mode until you have to manually post a quit message for the application.)You have probably guessed I compiled and ran the application. In result this is what happened. Luckily, to my knowledge, nothing serious has happened. If anyone knows another approach to this deadly loop. It is a great solution to this mystery. Thanks in advance.

Recommended Answers

All 2 Replies

The most common, and recommended, way of coding the message loop is like below, which was generated by VC++ 2005 Express when I created a windows project. Note that it is not necessary to use a break at all.

// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

why would you make it at the first place if you want to end it -.-"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.