Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

Catching the pointers of worker threads

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;
     Sleep(10000);
     for(i=0;i<1000;i++)
          sum+=i;

     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

     
     array.Add(AfxBeginThread(ExecThread,NULL,THREAD_PRIORITY_NORMAL+1));
}

void CTestKillThreadDlg::OnButton2()
{
     // TODO: Add your control notification handler code here
         try
        {
     CWinThread* thread=(CWinThread*)array.GetAt(0);
     TerminateThread (thread->m_hThread ,0);
 array.RemoveAt(0);
        }catch(...)
        {
                      array.RemoveAt(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


thanks
sudhakar
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial