Re: BN_CLICKED Event Handler in Background
Hi Scott,
I think it depends on how long you plan to sleep. I use Sleep() at times
when I want to wait a second or less. The time is pretty minimal and the
user can't tell that the delay is really there. You're right though.
Waiting for a minute would be very annoying.
Tom
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:zrednf6n2940wfDbnZ2dnUVZ_qGjnZ2d@comcast.com...
(1) Using Sleep in the worker thread is very clumsy because then you don't
have any way to make the thread shut down when the program is closing. The
proper approach is to call WaitForMultipleObjects within your worker
thread loop. This function gives you a sleep, plus a timeout to control
how long to sleep, plus a signaling mechanism so you can wake the thing up
early when needed.
DWORD res = WaitForMultipleObjects(count, handles, FALSE, 60000);
'handles' is an array of handles that you initialize in your main program
using CreateEvent. The first handle should be used to command the thread
to shut down when you want to close the program. The main thread will
call SetEvent on this handle. (Usually this is done in OnClose or
similar.) Other handles can be used to tell the thread to do this or
that.
The WaitForMultipleObjects call will wake up (i.e. return) with
WAIT_OBJECT_0. The thread code checks for this and returns, ending the
thread.
(2) Have the thread post user-defined messages to the main window,
reporting whatever progress you like in wParam/lParam. The main window
message handler keeps track of the progress and displays whatever you
like. To display progress use a dialog, not a message box. Example of
interthread messaging in the MFC FAQ here:
http://vcfaq.mvps.org/
--
Scott McPhillips [MVP VC++]