?
Solved

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

Posted on 1998-08-10
6
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 total points
ID: 1320512
try to use ExitProcess() as  it will ensure that all the child threads are dead before exiting.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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