Re: How to current close window while messages still in queue
On Wed, 5 Nov 2008 12:37:28 -0800, "David Ching"
<dc@remove-this.dcsoft.com> wrote:
"vvf" <vvf@vvf.com> wrote in message
news:e1$cRYqPJHA.5080@TK2MSFTNGP03.phx.gbl...
for (int i = 0; i <= iUpperBound && (WaitForSingleObject(m_hQuitEvent, 0)
== WAIT_TIMEOUT); ++i)
One optimization I did not see yet mentioned is that you can replace the
WaitForSingleObject(m_hQuitEvent, 0)
with a simple bool variable, e.g.
for (int i = 0; i <= iUpperBound && !g_bQuit; ++i)
No need to use an event. Just set the global bool variable g_bQuit to true
when you want the thread to quit.
That's not so much an optimization as it is an alternative technique. To
see what I mean, consider that the OP may be using the event elsewhere in
WFMO calls. If he is, he should continue using the event everywhere.
That said, if you use the bool technique, you must declare it volatile. For
anyone who doesn't appreciate why, see my discussion with Tommy in the
thread, "Should I use mutex in this context?" in
microsoft.public.vc.language, which was taking place just a couple of days
ago. See my reply (on 10/25) to the OP in that same thread for a good
overview of the various approaches to the problem and the issues and/or
advantages they have. In particular, event objects come in really handy
when you have other objects to wait on and need an escape valve for an
otherwise infinite wait.
--
Doug Harrison
Visual C++ MVP