Solved

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

Posted on 1998-08-10
6
205 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 218
how do i create updater to My Activex application? 3 94
Hibernate methods 2 77
matchUp  challenge 9 115
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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