• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

How to test if a thread has ended

Hello all,

     I have a CWinThread derived class that I am using.  Occationally while the thread is running I will post it thread messages using PostThreadMessage() which works fine.  The object is deleted when the threads finishes the Run() function because I have set m_bAutoDelete = TRUE.  However, if the thread ends before I expected it to, I get an error because I am trying to PostThreadMessage to an object that no longer exists.

    How can I test to see if PostThreadMessage is going to cause an error because the object is deleted?

Thanks
0
jymmealey
Asked:
jymmealey
  • 4
  • 4
1 Solution
 
jymmealeyAuthor Commented:
Here is a some code to explain what I am talking about

m_pPingThread = (Ping_Thread*) AfxBeginThread(RUNTIME_CLASS(Ping_Thread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
m_pPingThread->ResumeThread();

while (true) {
     m_pPingThread->PostThreadMessage(WM_USER + 1, 0, 0);
     Sleep(1000);
}

Once the "Ping_Thread" ends I will get an error trying to post at thread message
0
 
jkrCommented:
You could use

DWORD dw;
GetExitCodeThread(m_pPingThread->m_hThread, &dw);

if ( STILL_ACTIVE == dw) {

// still running
}
0
 
jymmealeyAuthor Commented:
Won't that still error because I am still trying to access the member of a deleted object?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
jkrCommented:
Sorry, that's of course correct. Do you really net to set it to ''AutoDelete'? I.e.

while (true) {
    DWORD dw;
    GetExitCodeThread(m_pPingThread->m_hThread, &dw);

    if ( STILL_ACTIVE == dw) {

      // still running
    m_pPingThread->PostThreadMessage(WM_USER + 1, 0, 0);

    } else {

        m_pPingThread = NULL;

        break;
    }

    Sleep(1000);
}

would seem a lot 'healthier' to me (the above assumes 'm_bAutoDelete == FALSE')
0
 
jymmealeyAuthor Commented:
I don't have to but how do I get the object to delete after I am done with it?
0
 
jkrCommented:
>>how do I get the object to delete after I am done with it?

Argh, should re-read my code snippets before submitting, that I left out:

while (m_pPingThread) {
   DWORD dw;
   GetExitCodeThread(m_pPingThread->m_hThread, &dw);

   if ( STILL_ACTIVE == dw) {

     // still running
   m_pPingThread->PostThreadMessage(WM_USER + 1, 0, 0);

   } else {

       delete m_pPingThread;
       m_pPingThread = NULL;

       break;
   }

   Sleep(1000);
}
0
 
jymmealeyAuthor Commented:
I tried that but I am getting a complier error saying
error C2248: 'Ping_Thread::~Ping_Thread' : cannot access protected member declared in class 'Ping_Thread'

Do I just need to move the desctructor to a private rather than protected?

0
 
jkrCommented:
>>Do I just need to move the desctructor to a private rather than protected?

No, better move it to the 'public:' section.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now