Handling messages posted during a shutdown process

Ok, still working on a fun little multi-threaded SDI program I've been
trying to get up and running, and for the most part everything is
working fine.

I'm currently working on the shutdown process, but an interesting catch
happened:

Upon shutting down the collector / processing threads, they post
additional messages to the main window (in this case a CFrameWnd).

Some of these messages contain information that needs to be dealt with
before shutting down, so my solution was to have a boolean that
determines whether or not it's actually safe to shutdown, as below:

void CMainFrame::OnClose()
{
	c_Interface.ShutdownCollectors();
	c_Interface.ShutdownConsumers();
	if (m_bCanShutdown)
	{
		CFrameWnd::OnClose();
	}
	else
	{
		m_bCanShutdown = true;
		PostMessage(WM_CLOSE);
	}
}

This way, the final message posted that I care about is WM_CLOSE. It
seems to work fine, but I was looking for some expert opinions to tell
me if there's a better way or something I'm missing.

Thanks,
Josh McFarlane

0
darsant (187)
8/11/2005 10:58:19 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
336 Views

Similar Articles

[PageSpeed] 1

"Josh McFarlane" <darsant@gmail.com> wrote in message 
news:1123801099.177094.84430@g47g2000cwa.googlegroups.com...
> Ok, still working on a fun little multi-threaded SDI program I've been
> trying to get up and running, and for the most part everything is
> working fine.
>
> I'm currently working on the shutdown process, but an interesting catch
> happened:
>
> Upon shutting down the collector / processing threads, they post
> additional messages to the main window (in this case a CFrameWnd).
>
> Some of these messages contain information that needs to be dealt with
> before shutting down, so my solution was to have a boolean that
> determines whether or not it's actually safe to shutdown, as below:
>
> void CMainFrame::OnClose()
> {
> c_Interface.ShutdownCollectors();
> c_Interface.ShutdownConsumers();
> if (m_bCanShutdown)
> {
> CFrameWnd::OnClose();
> }
> else
> {
> m_bCanShutdown = true;
> PostMessage(WM_CLOSE);
> }
> }
>
> This way, the final message posted that I care about is WM_CLOSE. It
> seems to work fine, but I was looking for some expert opinions to tell
> me if there's a better way or something I'm missing.
>

If c_Interface::ShutdownCollectors() and c_Interface.ShutdownConsumers() 
posts messages synchronously before returning, then they are guaranteed to 
arrive at CMainFrame before your posted WM_CLOSE, so it should work well.

You should move the if (m_bCanShutdown) check up, to guard against calling 
the Shutdown methods again when handling your posted WM_CLOSE.

Cheers,
David
http://www.dcsoft.com



0
dc2983 (3206)
8/12/2005 12:25:52 AM
On 11 Aug 2005 15:58:19 -0700, "Josh McFarlane" <darsant@gmail.com> wrote:

>Ok, still working on a fun little multi-threaded SDI program I've been
>trying to get up and running, and for the most part everything is
>working fine.
>
>I'm currently working on the shutdown process, but an interesting catch
>happened:
>
>Upon shutting down the collector / processing threads, they post
>additional messages to the main window (in this case a CFrameWnd).
>
>Some of these messages contain information that needs to be dealt with
>before shutting down, so my solution was to have a boolean that
>determines whether or not it's actually safe to shutdown, as below:
>
>void CMainFrame::OnClose()
>{
>	c_Interface.ShutdownCollectors();
>	c_Interface.ShutdownConsumers();
>	if (m_bCanShutdown)
>	{
>		CFrameWnd::OnClose();
>	}
>	else
>	{
>		m_bCanShutdown = true;
>		PostMessage(WM_CLOSE);
>	}
>}
>
>This way, the final message posted that I care about is WM_CLOSE. It
>seems to work fine, but I was looking for some expert opinions to tell
>me if there's a better way or something I'm missing.

Do your two Shutdown calls run to completion, such that all messages are
posted by the secondary threads before they return? If not, then you have a
race with your PostMessage.

Also, your (!m_bCanShutDown) path returns you to your message loop, and
this normally brings your entire program back into play. This is important,
because between the time OnClose is called and you make your PostMessage
call, other messages you don't want to handle can get queued up. I talk
about ways to deal with that sort of thing here:

http://members.cox.net/doug_web/threads.htm

See Q5 and later for details.

-- 
Doug Harrison
VC++ MVP
0
dsh (2498)
8/12/2005 2:33:47 AM
David Ching wrote:
> If c_Interface::ShutdownCollectors() and c_Interface.ShutdownConsumers()
> posts messages synchronously before returning, then they are guaranteed to
> arrive at CMainFrame before your posted WM_CLOSE, so it should work well.
>
> You should move the if (m_bCanShutdown) check up, to guard against calling
> the Shutdown methods again when handling your posted WM_CLOSE.

Doug Harrison [MVP] wrote:
> Do your two Shutdown calls run to completion, such that all messages are
> posted by the secondary threads before they return? If not, then you have a
> race with your PostMessage.
>
> Also, your (!m_bCanShutDown) path returns you to your message loop, and
> this normally brings your entire program back into play. This is important,
> because between the time OnClose is called and you make your PostMessage
> call, other messages you don't want to handle can get queued up. I talk
> about ways to deal with that sort of thing here:
>
> http://members.cox.net/doug_web/threads.htm
>
> See Q5 and later for details.

Both Shutdown commands wait on the threads to terminate before deleting
them and returning, so once each function has returned, the threads are
shutdown and no longer transmitting messages to the queue. I don't
believe any race conditions should occur. Also the shutdown threads
simply return if all threads are shutdown so no harm in calling it
twice.

As far as internally there isn't any other messages besides those
unprocessed from the thread that I care about when shutting down,
although you brought up an interesting point - I'll need to make the
program enter a GUI shutdown state so the user can't do anything except
let the program terminate.

Thanks for both your suggestions,
Josh McFarlane

0
darsant (187)
8/12/2005 1:36:01 PM
Reply:

Similar Artilces:

OL2007, messages don't always close after sending
Let's say I've just finished a spreadsheet and I want to send it to someone. I'll click the big Office button and choose Send -> Email, and up pops a new email message with my spreadsheet attached. All fine so far until I click the Send button (or Alt-S, or Ctrl-Enter, doesn't matter which). The message appears in the Outbox and Outlook will send it, but it also stays open in front of Excel until I click the Close button. I can still make further edits to my workbook, but the email message is in the way and I can't see what I'm doing, until I manually close...

option "send picture with messages"
Hi ... where can i find the "send picture with messages" option in MS Oulook 2007 ? in Outlook Express you can find this option send picture with messages" in Tools -> Option -> Send -> HTML Settings -> send picture with messages option .... BUT in Oulook 2007 i couldn't find it .. so canyou please where can find it .. Thank You That is a registry only option now. See http://www.msoutlook.info/question/72 -- Robert Sparnaaij [MVP-Outlook] Coauthor, Configuring Microsoft Outlook 2003 http://www.howto-outlook.com/ Outlook FAQ, HowTo, Downl...

moving messages from outlook express to outlook
I am trying to move messages from outlook express version 6 to outlook 2003. When I export from outlook express, there is no indication where the export file is stored. Is there a way to move messages or will I have to email them to myself? Thanks ...

C00D11B3 error message
I have been using Media player for years, now have ver. 11, in the past two day, I have had a problem if I go to player and play some music stored on the hard drive, it works. I f I attempt to play something on the internet I get the error and then it won't play anything until I reboot the system. I thought it was a firewall issue, but can't figure out what it would be. I also have checked connect to the internet override, without success. I have recently changed routers to a linksys with Network Magic set to basic. I have no idea what is causing the issue. -- J P...

Overrides vs. Messages
Hi I have a basic question about MFC. There are 4 sections in Properties windows -Properties -Events -Messages -Overrides For example: For CView class OnDraw is in overrides section. But OnDrawItem is in messages section. Why is it so? Some methods are in mesages section(handle with message map) some are virtual. What is the logic behind this separation? Thank you. In a nutshell, the override section give you access to the virtual function of that class, and the messages sections give you access to the messages that are not already mapped to a virtual function in that class. I hope that ...

Messages not delivered to another Routing Group HELP NEEDED
I have three routing groups ( exchange in the 3 of them are Exch 2k sp3 ). Mail from one server ( in my headquarter routing group ) to another ( in one branch office routing group ) is getting stucked in MESSAGES WITH UNREACHABLE DESTINATION and not routed to that server. Connectors seems to be working just fine. After add my other exchange server from my headqurter group to the connector to be albe to send mail it shows the queue now in the connector but does not sends anything. Otherwise, mail from my branch office to my headquarter correctly sends mail. Any help would be gre...

Very Slow Transaction Post
My POS station has become incredibly slow to post transactions and print the receipt. The database has grown to about 2gigs. Any suggestions? Thanks Scott Hi Scott Normally we find that growth is mostly due to the journals. I would recommend that you thin them out a bit, say removing anything older than a year. You could use the below command for it: DELETE Journal WHERE time < getdate()-365 The above statement will delete all journals over 12 months old (alter the number of days as required) and then run this SQL statement dbcc shrinkdatabase DatabaseName -- Jenny "...

WindowProc: combobox messages problem
In my CControlBar I have overridden the virtual WindowProc. The combobox on the controlbar displays correct and behaves well in runtime. Now, I spent a couple of hours trying to get some messages that I need, using WindowProc. This is needed because CControlBar will pass everything on to the controlbar owner, and that's not what I want. WindowProc is my way to intercept those messages. I already found out that the messages are, ehm, weird: their numbers do not match on related defines in winuser.h. To give an example: WM_COMMAND, 0x0003 functions as a WM_SETFOCUS. But WM_SETFOCUS is def...

Viewing messages #2
When I get e-mail and try to view the messages, they never come up. Can someone help me? Not yet. State your Outlook version, the type of mail transport you are using, whether you are using a preview pane or opening the message, etc. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus name here) virus, all mail sent to my personal account will be deleted without reading. After searching google.groups.com and finding no answer, Wleef33 asked: | When I get e-mail and try to view the messages, they | never come ...

Import Messages from Outlook Express 6
Hi, I would like some advice on importing messages that are currently on a Win98se machine running OE 6 into an XP Pro machine running Outlook 2003, Thanks, any help greatly appriciated, Ed <anonymous@discussions.microsoft.com> wrote in message news:5c9c01c3e5bb$834ea400$a001280a@phx.gbl... > Hi, > I would like some advice on importing messages that are > currently on a Win98se machine running OE 6 into an XP > Pro machine running Outlook 2003, > Thanks, any help greatly appriciated, > Ed Go here to backup your old OE: http://insideoe.tomsterdam.com/backup/index.htm...

Message tracking not work on XP SP2
Message tracking is not working on Windows XP SP2. I've tried System Manager 2000 and 2003 with the same result. Message searching will started, but is still running without result. After uninstalling SP2 it's working fine! On Wed, 19 Oct 2005 05:26:05 -0700, Pol <Pol@discussions.microsoft.com> wrote: >Message tracking is not working on Windows XP SP2. I've tried System Manager >2000 and 2003 with the same result. Message searching will started, but is >still running without result. After uninstalling SP2 it's working fine! Your workstation needs to be abl...

certin messages block POP download.
Hi all, I have an Exchange 2003 server with @ 300 users with many virtual domains. Problem I have is that one domain in particular has certin messages getting stuck while downloading and have to be cleared in Webmail in order to free the mailbox. They mainly seem to be HTML messages but there have been two text ones from another Virtual Domain. I am stumped. I get no errors in the logs and have turned on diagnostics logging to watch whan it is happening and do not see anything. I have elimantated Outlook by trying Outlook Express at the same thing happens. They get sometimes as many a...

Weird black screen when responding to message
Hi All, The title says it all really. I've recently started to get a black screen with coloured type when I reply to messages. Invariably the screen goes back to white with whatever colour type is applicable to a string of messages but sometimes I have to hit send with it showing coloured text on black. When the message arrives at the other end the colour is correct. More oddly, this doesn't happen on all messages but is beginning to become more frequent. I've run a couple of spyware progs and Norton anti virus etc. so don't think its anything nasty behind it. So over to...

Sorting messages by sender
I've recently changed from using Outlook 2003 on an old Celeron PC running Win 2k, to a new P4 machine with XP. On the old set-up, I could click on the 'From' column header to sort messages in a mail folder by sender, then start typing the name of the sender to quickly take me to the messages from that person. This was very useful and I used this feature regularly, although it only seemed to work for the first three characters of the name, the fourth character entered would then take the cursor to the first instance of a name beginning with this letter. This was fine as the firs...

Multiple messages
Hi I am receiving the same messages a number of times to my outlook express mail box. Sometimes I have 5 or 6 of the same message. Does anyone know why this is, or how I can stop it? Thanks very much Try posting this in an Outlook Express news group - this is not one of them. Outlook is a part of Microsoft Office and is what this group supports. Outlook Express is a part of Internet Explorer and has its own news groups. You can also find some good Outlook Express information here: http://www.insideoutlookexpress.com/ --� Milly Staples [MVP - Outlook] Post all replies to the group...

Intelligent Message Filtering
Hi All, Just to say what a good product IMF is. All my Junk-Mail is archived in a local queue. My dream become true ! No more wasted time in explaining what a SPAM is...... Diego P Castro MCSE-MCSA-SPS8 Brasil ...

Error Message 0x8007007F
Clip organizer keeps giving this error message when opened: Error Message 0x8007007F It will not open or insert pictures!!! Would appreciate any and all help junk2bj@hotmail.com http://www.mvps.org/msauer/clip_organizer_support.htm -- Mary Sauer MS MVP http://office.microsoft.com/ http://www.mvps.org/msauer/ news://msnews.microsoft.com "BJ" <junk2bj@hotmail.com> wrote in message news:38ec01c42980$ce5312c0$a301280a@phx.gbl... > Clip organizer keeps giving this error message when > opened: > > Error Message 0x8007007F > > It will not open or insert pictu...

Outlook does not refresh imap inbox when message is deleted on ser
I have Outlook set up to use imap on two computers as well as my Palm. The problem is that messages deleted from the server will not dissappear in Outlook as long as Outlook is running. If if restart Outlook the imap inbox is updated correctly. I thougt Microsoft had solved this problem between Outlook 2000 and 2003, but obviously not. Is there a solution? You will need to manually purge the items using Edit->Purge. (I think it is on the Edit menu - I despise IMAP so I don't have a current setup to check.) --� Milly Staples [MVP - Outlook] Post all replies to the group to keep ...

Error message #3
Hi, everyone, I open to MS Excel 97, the following error messages was shown on screen. 26146100: file format is not valid. Please help Thanks Jimmy Something that is being opened is causing trouble. It could be an addin checked in Tools|Addins or a file in your XLStart folder (or if you are using tools|options|General|"Alternate Startup File location", it could be in there.) (Not many use the alternate startup file location, so try erasing that value from that box in excel--don't delete the real folder. If that fixed it, you're done.) If not, your task will be to...

Error message 0x800ccc13 #4
Does anyone know what error message #0x800ccc13 means. I get it when I hit the sned/receive button. I can send email through Outlook 2000 but I can't receive anything. The message says that a TCP/IP error occured while trying to send data to the serever. All of my server set up info appears correct. I appreciate any answers ...

Cannot view print preview of HTML messages after IE7 uninstall
I can no longer view a print preview of HTML messages in OUtlook 2003 after uninstalling Internet Explorer 7 Beta 2. Anyone know how to fix this? Have you tried reinstalling IE7 Beta 2? Patrick Schmid "QaT" <QaT@discussions.microsoft.com> wrote in message news:F8662AD3-5552-4B70-967C-E602E792C439@microsoft.com... >I can no longer view a print preview of HTML messages in OUtlook 2003 after > uninstalling Internet Explorer 7 Beta 2. Anyone know how to fix this? Yes, and still does not work. Very annoying. Might Windows Repair fix this issue? "Patrick Schmid&...

Launch folder windows in separate processe
The setting as in subject selected under Control Panel -> Folder Options More than one windows explorer open. Each of them opened in other folder. One of them opened for search function. However, if investigating with Task Manager all windows explorer windows lead to the same explorer process (task right click -> Go to process). The list "Processes" of task manager presents only one explorer window. So the setting as mentioned above not really functional. What's the reason? kakii wrote: > > The setting as in subject selected under Control Panel ...

Server settings for message addressee limit
How do you check/change the Exchange Server message addressee(To)limit? When sending messages to servaral recipients, some names are cut out. Thanks. ...

Why i keep on getting same message from mail server?
I'm using Microsoft Outlook, and have been getting message from server. However, i have been keep on getting same message whenever i re-start the p.c... is there any solulation on this matter? Hi Hide, your Outlook version is? Have a look on this sites: "Duplicate e-mail messages are downloaded from a POP3 e-mail server account in Outlook 2003" http://support.microsoft.com/kb/885870/en-us "Why does my Outlook keep receiving duplicate messages?" http://www.comentum.com/outlook-issues.html Maybe it helps. -- Oliver Vukovics Share your Outlook PST files without Ex...

Outbound message queue #2
Hi I have Exchange 5.5 :( In the Internet Mail Service, Queues, Outbound messages awaiting delivery, there are hundreds of emails that look like SPAM. I know our Exchange Server is not a mail relay so where are all of these emails coming from and how do I stop them. I tried turning on all the message tracking and log file options I could find and looking at the log file but it didn't really show anything obvious except the domain "mail.ru" was on a like 30 messages. (I put "mail.ru" in the domain reject list) Can someone help me please................... Tha...