Re: Question about processing messages during exit
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/23/04
- Next message: Joseph M. Newcomer: "Re: DOS applications ?"
- Previous message: Joseph M. Newcomer: "Re: data exchange between two windows"
- In reply to: Damien Cymbal: "Re: Question about processing messages during exit"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 22 May 2004 23:46:38 -0400
Same problem. You have to inhibit the actual close until all pending messages are sent.
You have to make sure that all messages you want to have queued up are queued up before it
is posted. Note that DefWindowProc does NOT post a quit message (one of the most common
failure modes when writing a Win32 API program was forgetting to PostQuitMessage, usually
on the WM_NCDESTROY or WM_DESTROY handler of the main frame window).
As far as I can tell, a WM_QUIT message is posted (via AfxPostQuitMessage) when an OnClose
happens on the main window, or OnNcDestroy happens on the main window. I didn't follow all
the nested conditionals logic, so you may want to look at it more closely (search for
PostQuitMessage in the MFC source).
So you don't need to do anything special; you can "eat" the WM_CLOSE message simply by not
calling the superclass method. And failing to dispatch the messges would be fatal, since
the messages may be required to handle shutdown properly.
If your message sequence is "non-convergent", that is, processing messages create more
messages, then your messaging structure is deeply flawed and has to be corrected. You
can't have a nonconvergent sequence if you want to get a shutdown.
Never try to build complex solutions to simple problems. To paraphrase Einstein, a
solution should be as complex as necessary, but no more complex. Create message loops,
invoking PumpMessage, doing your own dispatching, etc. are all more complex than a
solution needs to be.
joe
On Sun, 23 May 2004 00:34:02 GMT, Damien Cymbal <d_cymbal@hotmail.com> wrote:
>Joseph M. Newcomer <newcomer@flounder.com> wrote in
>news:o2ita0hj5i43ba6ubaf1dqk8tc4974es0j@4ax.com:
>
>> The you are handling your shutdown badly.
>>
>> You must not shut down your main GUI thread until all the messages
>> have been processed.
>
>Thanks. What about the case where there are no threads involved? Say I am
>in OnClose() and as part of my normal close processing a new essage gets
>posted to the window (say some sort of debug message for example). This is
>from the main thread, so I can't use the technique of maintaining a thread
>count and relying on an exiting thread to kickstart my close by posting
>another message. It seems like in this case, I would need to eat the
>WM_CLOSE, pump messages (but probably not dispatch them since that might
>beget more messages and I'll never exit) until I'm "satisfied" and then
>repost the close request. In my case I have an unrelated problem I am
>trying to fix and I need one of these posted debug messages. That is what I
>am losing. There actually aren't any additional threads running in my app
>at shutdown.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
- Next message: Joseph M. Newcomer: "Re: DOS applications ?"
- Previous message: Joseph M. Newcomer: "Re: data exchange between two windows"
- In reply to: Damien Cymbal: "Re: Question about processing messages during exit"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|