Solved

How can I remove all created thread when the main app exit?

Posted on 1998-08-10
6
201 Views
Last Modified: 2013-11-20
In my program, I create some threads of same type.
Killing thread during the main app running has no problem.
I use ::PostThreadMessage API to quit thread.
To destroy all thread when the main app quit, I write a code
to CMainFrame::OnDestroy() like that:

CServerThread * pServerThread;
while (false == m_ClientList.IsEmpty()) {
      pServerThread = m_ClientList.RemoveHead();
      ::PostThreadMessage(
            pServerThread->m_nThreadID,
            WM_USER_QUITMESSAGE,
            0,
            0
      );
}

But, it cause a object dumping at some time.
How do i clear this problem?

I use user-interface thread that derived from CWinThread class,
and start thread as this form:

ASSERT(hWnd = GetSafeHwnd());
pServerThread = new CServerThread(hWnd, pListenSocket, m_nClientID);
if (NULL == pServerThread)
      return;
if (NULL == pServerThread->CreateThread())
      return;

Please someone answer me...
0
Comment
Question by:WestIn
6 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1320510
Try inserting the following line after your PostThreadMessage() call:

WaitForSingleObject(pServerThread->m_hThread,INFINITE);

This waits until the thread pServerThread has terminated.
Otherwise i think there are two samples included with the MSVC distribution, showing how to
terminate threads(a fast and a slow method). I have not tried them yet, but i think they should
help,too.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1320511
Try to put your codes under the OnClose() function instead of OnDestroy().
0
 
LVL 1

Accepted Solution

by:
arun_ta earned 100 total points
ID: 1320512
try to use ExitProcess() as  it will ensure that all the child threads are dead before exiting.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:jonalee
ID: 1320513
I think the coding is some OK. If sometime it will cause object dumping, you can keep the ThreadHandle (CWinThread.m_hThread), not the CWinThread Object. On the OnDestory function, you can call TerminateThread (hThread,dwExitCode) to destroy all thread. It's very secure that you call TerminateThread for a non-existing handle. it will not cause any problem.

Hope It helps
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1320514
TerminateThread() is secure in the way you mentioned - but all thread's data is not released
( HWND's, HBRUSH's and so on ...)
At least that is what is said in my documentation (MSVC 4.0)
0
 

Expert Comment

by:dreamer053197
ID: 1320515
TerminateThread() is a very dangerous function since it'll not release certain types of data as 'snoegler' mentioned. If the thread being terminated is running inside a critical section within the kernel, it can be really messy. ExitProcess() is not much better for a multithreaded process since it simply calls TerminateThread() to kill all threads. So your PostQuitMessage is properly the best bet. Just curious, why are you calling CreateThread() for a user-interface thread?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 103
Generic progress indicator 6 115
sum28 challenge 31 111
has77  challenge 9 82
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now