Re: BN_CLICKED Event Handler in Background

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



vv_ramana@xxxxxxxxx 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++]

.



Relevant Pages

  • Re: BN_CLICKED Event Handler in Background
    ... I use Sleep() at times when I want to wait a second or less. ... 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. ... 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. ...
    (microsoft.public.vc.mfc)
  • Re: 2 different threads checking on same variable scenario
    ... Because you are implementing a console application here, it seems to me that a third way to implement this would be to simply have the main thread call some "do work" function repeatedly. ... Depending on just what you intended to do in your "do progress indicator thing" loop, there may be a fourth option. ... That is, use an auto-reset wait event to block the main thread between progress updates, a flag to indicate when the work has been done, and then have the worker thread set the flag when it's done, and intermittently set the wait event to indicate to the main thread to update the progress. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: BN_CLICKED Event Handler in Background
    ... Using Sleep in the worker thread is very clumsy because then you ... Have the thread post user-defined messages to the main window, ... reporting whatever progress you like in wParam/lParam. ... it opens the dialog but will never exit from this function!! ...
    (microsoft.public.vc.mfc)
  • Re: 2 different threads checking on same variable scenario
    ... it depends on what you intend to do in your main thread's loop. ... Depending on just what you intended to do in your "do progress indicator ... indicate when the work has been done, and then have the worker thread set ...
    (microsoft.public.dotnet.languages.csharp)
  • Worker Threads
    ... I have an application with an import routine for converting data files. ... One approach is to have my OnFileImport callback create the Status Dialog ... and have the dialog create the worker thread to actually process the data. ... The thread could then post messages back to the dialog to update the progress ...
    (microsoft.public.vc.mfc)