Click to See Complete Forum and Search --> : Event when thread ends?
Runt888
August 24th, 2005, 12:07 PM
I'm writing an GUI application that needs to do some lengthy processing. I've got my main thread handling the UI and the event loop. When it needs to do some processing, it creates a new thread to do it. The thread does the processing, and updates some shared variables so the main thread can display a progress bar. This all works fine.
However, now I need to run some code in the main thread when the second thread is finished. Is it possible to have the second thread somehow notify the main thread that it is finished? The only way I can think to do it is to fire a user message to the main window. Is there another way?
Kelly
PS - I'm using WIN32 API only, and creating the thread with CreateThread.
Andreas Masur
August 24th, 2005, 12:45 PM
However, now I need to run some code in the main thread when the second thread is finished. Is it possible to have the second thread somehow notify the main thread that it is finished? The only way I can think to do it is to fire a user message to the main window. Is there another way?
Although there are one or another different way of doing it...I would also suggest the message mechanism. Any objections or just a general question to evaluate all possible ways?
Runt888
August 24th, 2005, 12:47 PM
Thanks for the reply. I'm fine with using messages...I was just curious if there were any other ways of doing it. For example, if I was threading a console program without a message loop.
Thanks again for the reply!
Kelly
NigelQ
August 24th, 2005, 12:52 PM
You can signal an event (synchronization object) to notify the first thread of some activity in the second thread.
See:
CreateEvent
SetEvent
ResetEvent
WaitForSingleObject
When the second thread needs to signal something to the first, it can simply call SetEvent on the handle. The first thread can either be waiting for the signal or can periodically check it, as needed.
To periodically check it, use WaitForSingleObject with 0 wait time.
Hope this helps,
- Nigel
Andreas Masur
August 24th, 2005, 01:24 PM
For console applications there are other approaches that can be used as already indicated. Nevertheless, you still can use messages within it as well.
Other than that...you didn't specify your application flow, however, if it is the following
Create workerthread from main thread
Let the main thread wait for the termination of the workerthread
Do some stuff
then you don't need threads in the first place. Threads are being used to parallelize different tasks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.