Re: BN_CLICKED Event Handler in Background
vv_ramana@yahoo.com wrote:
Thanks. So far so good. I made some good progress yesterday. However,
I have couple of questions and need you expertise.
1. How do we make the "Worker Thread" to do the operation every 1
minute (I.e. Add Delay or Sleep).
2. When this operation is running in the background, the user should
not be allowed to logout.
How can I show a message box (just like MessageBox or
AfxMessageBox) which will keep checking the
the progress of the background operation and close the dialog
itself once done and proceed with the logout?
Thanking you in advance.
- Newbie
(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++]