Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-08-10
6
Medium Priority
?
224 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
Industry Leaders: 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!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

609 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