Re: Question about processing messages during exit

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/23/04


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



Relevant Pages

  • Re: Unable to shutdown properly
    ... Dana Cline - MVP ... > I have Windows XP home Service pack 1. ... > When I click on shutdown, the window pops up to ask if I want to shut ...
    (microsoft.public.windows.mediacenter)
  • Re: Windows XP shutdown
    ... seconds to 2 minutes before next window comes up ... Shutdown issues are generally caused by a program and/or process that is ... or can be legitimate (such as an invasive antivirus like Norton or McAfee). ... If troubleshooting the issue is too difficult - and there is ...
    (microsoft.public.windowsxp.general)
  • Re: PC hangs when shutdown, svchost.exe uses 90-99% cpu + other pr
    ... The Group Policy client-side extension Security failed to execute. ... If anything is visible in this window, copy the information and post it in a reply. ... This will allow any blue screen error messages to be displayed. ... Computer hangs during shutdown and NEVER comes out of it (this ...
    (microsoft.public.windowsxp.help_and_support)
  • Shutdown in 60 seconds; service.exe error
    ... The window pops up within 30 seconds of restarting / booting the ... This does not affect the lsass.exe file, and I had the worm before, ... oddly enough without the 60 second shutdown window. ... run them from the disc in safe mode? ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Terminating thread does not signal.
    ... >>I have a progress control that uses a thread to update the ui. ... >>window is being shutdown, I set an event to let the thread know it should ... I then wait for the thread to terminate to continue the shutdown. ... > window functions use SendMessage under the hood, ...
    (microsoft.public.vc.mfc)

Loading