Click here to Skip to main content
15,390,695 members
Articles / Desktop Programming / MFC
Posted 5 Sep 2002


40 bookmarked

Waiting for a thread to terminate

Rate me:
Please Sign up or sign in to vote.
4.60/5 (21 votes)
5 Sep 2002CPOL1 min read
How to pause the main thread until a secondary thread is finished


A few weeks ago, I had to program a custom dialog-box as part of a MSI-Setup. The code for this has to be distributed as a Regular-DLL. This dialog-box makes some actions during the initialisation and depending of the result, I had to show or not this box. The actions made were done with others threads. The problem was to wait these actions to be terminated, to first analyse the result, and then display or not the box.

The first method I've found was very simple and worked very well, but only when this DLL was used from another "normal" application. When this DLL was called from a MSI-Setup, everything went wrong.

Then I requested some help from Microsoft which gave me a few days later a solution that didn't work too!! But it gave me a good idea, and finally I found the right solution, which works on every OS and regardless which kind of application calls this DLL. It works also on multiprocessor machines.

The solution

You've to write two functions. The first one, void CMyTestDialog::PeekMessageLoop() will acquire the messages from the message queue:
void CMyTestDialog::PeekMessageLoop()
    MSG msg;
    while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))

The second one, void CMyTestDialog::WaitForThreadToTerminate(HANDLE hThread) will take the handle of the thread we are waiting for:

<PRE lang=c++>void CMyTestDialog::WaitForThreadToTerminate(HANDLE hThread)
    DWORD dwRet;
        dwRet = ::MsgWaitForMultipleObjects(1, &hThread, FALSE, 
        if (dwRet != WAIT_OBJECT_0)
    } while ((dwRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED));

Sample code

Suppose you have a dialog-box with a button. When you click on it, your DLL launches an action in a secondary thread, and have to wait until this action is done. The only thing you have to do is:
void CMyTestDialog::OnButton1() 
    m_pUpdateThread = AfxBeginThread(UpdateDeviceContent, 
    if (m_pUpdateThread)
//Do whatever you want after the action is finished

So I hope this article will help someone. The sample code in the zip-file is not very clean, but I think it's quite simple and everyone can understand it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
No Biography provided

Comments and Discussions

Generaljust wanna say thanks! Pin
sprikitik17-Feb-09 21:03
Membersprikitik17-Feb-09 21:03 
Generalgo what i wanted Pin
Balkrishna Talele24-Jan-06 19:06
MemberBalkrishna Talele24-Jan-06 19:06 
Generalexcellent articles Pin
spring12-Aug-05 15:39
Memberspring12-Aug-05 15:39 
GeneralThanks, bro Pin
Mingming Lu10-Jan-04 19:02
MemberMingming Lu10-Jan-04 19:02 
QuestionHow do I revise the sample code for waiting multiple threads Pin
jordan_chuang10-Mar-03 20:32
Memberjordan_chuang10-Mar-03 20:32 
AnswerRe: How do I revise the sample code for waiting multiple threads Pin
Ybbozman31-Mar-03 23:19
MemberYbbozman31-Mar-03 23:19 
GeneralThe thread handle should be copied Pin
Sheng-hung8-Dec-02 19:43
MemberSheng-hung8-Dec-02 19:43 
GeneralRe: The thread handle should be copied Pin
Ybbozman31-Mar-03 23:19
MemberYbbozman31-Mar-03 23:19 
GeneralRe: The thread handle should be copied Pin
ckTan8124-Mar-05 15:42
MemberckTan8124-Mar-05 15:42 
GeneralRe: The thread handle should be copied Pin
Sheng-hung1-Apr-05 14:11
MemberSheng-hung1-Apr-05 14:11 
GeneralRe: The thread handle should be copied Pin
ckTan813-Apr-05 14:39
MemberckTan813-Apr-05 14:39 
Generalthanks Pin
QiShouFeng7-Oct-02 23:34
MemberQiShouFeng7-Oct-02 23:34 
GeneralAtlWaitWithMessageLoop Pin
Ariel Rocholl13-Sep-02 6:33
MemberAriel Rocholl13-Sep-02 6:33 
GeneralRe: AtlWaitWithMessageLoop Pin
Ybbozman17-Sep-02 19:36
MemberYbbozman17-Sep-02 19:36 
GeneralRe: AtlWaitWithMessageLoop Pin
Ariel Rocholl23-Sep-02 6:38
MemberAriel Rocholl23-Sep-02 6:38 
GeneralRe: AtlWaitWithMessageLoop Pin
Ariel Rocholl23-Sep-02 6:39
MemberAriel Rocholl23-Sep-02 6:39 
GeneralRe: AtlWaitWithMessageLoop Pin
Ybbozman9-Oct-02 20:54
MemberYbbozman9-Oct-02 20:54 
QuestionWhat was the solution proposed my MS? Pin
Ernesto Perales Soto9-Sep-02 11:14
MemberErnesto Perales Soto9-Sep-02 11:14 
AnswerRe: What was the solution proposed my MS? Pin
Ybbozman10-Sep-02 3:23
MemberYbbozman10-Sep-02 3:23 
GeneralMSI setup DLL sample... Pin
Mario M.7-Sep-02 15:29
MemberMario M.7-Sep-02 15:29 
GeneralRe: MSI setup DLL sample... Pin
Ybbozman10-Sep-02 3:19
MemberYbbozman10-Sep-02 3:19 
Generalthanx Pin
Anonymous6-Sep-02 20:49
MemberAnonymous6-Sep-02 20:49 
GeneralIt looks good! Pin
Leafdown6-Sep-02 3:56
MemberLeafdown6-Sep-02 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.