Catching the pointers of worker threads

Posted on 2004-11-22
Last Modified: 2013-11-20
Hello Friends,

To kill the threads I am using following code idea in my application. After killing the threads also, I am seeing some of the threads are still running in my application

CObArray threadArray;

UINT ExecThread(LPVOID lp)
     int i=0,sum=0;

     CString str_sum;
     str_sum.Format ("%d",sum);
     AfxMessageBox("The sum is "+str_sum);
     return 0;    


void CTestKillThreadDlg::OnButton1()
     // TODO: Add your control notification handler code here


void CTestKillThreadDlg::OnButton2()
     // TODO: Add your control notification handler code here
     CWinThread* thread=(CWinThread*)array.GetAt(0);
     TerminateThread (thread->m_hThread ,0);
                           //  I put catch block here bcause, if  a thread that i am accessing here is already executed, then
                          //  TerminateThread (thread->m_hThread ,0); is raising exception, hence treating it as executed i am
                          //  removing that thread from array



what is the problem in my codes??

Ad also I need to catch the pointers of all worker threads that are still in memory

Question by:sudhakar_koundinya
    LVL 48

    Accepted Solution

    Using TerminateThread is wrong practice and should be used only for threads which are not responding. Thread should exit by natural way by returning from the thread function. I described this in one of your previous questions.
    Exception is raised because CWinThread object created by AfxBeginThread has m_bAutoDelete member TRUE, and CWinThread object deletes itself. To prevent this, create thread in suspended state, change m_bAutoDelete to FALSE and resume thread.
    I don't see full code in your post - where is loop which enumerates all worker threads?
    Maybe OnButton1 runs one thread, and OnButton2 kills first thread in array? So, you click once on button1 and once on button2, and see that one thread is still running. Maybe this is main application thread?
    LVL 13

    Assisted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    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…
    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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now