Solved

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

Posted on 1998-08-10
6
195 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
Comment Utility
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
Comment Utility
Try to put your codes under the OnClose() function instead of OnDestroy().
0
 
LVL 1

Accepted Solution

by:
arun_ta earned 100 total points
Comment Utility
try to use ExitProcess() as  it will ensure that all the child threads are dead before exiting.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:jonalee
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 discusses moving either the default database or any database to a new volume.

743 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

10 Experts available now in Live!

Get 1:1 Help Now