Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to test if a thread has ended

Posted on 2005-04-14
8
Medium Priority
?
273 Views
Last Modified: 2013-11-20
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
Comment
Question by:jymmealey
  • 4
  • 4
8 Comments
 
LVL 2

Author Comment

by:jymmealey
ID: 13785343
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
 
LVL 86

Expert Comment

by:jkr
ID: 13785408
You could use

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

if ( STILL_ACTIVE == dw) {

// still running
}
0
 
LVL 2

Author Comment

by:jymmealey
ID: 13785466
Won't that still error because I am still trying to access the member of a deleted object?
0
Technology Partners: 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 86

Expert Comment

by:jkr
ID: 13785525
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
 
LVL 2

Author Comment

by:jymmealey
ID: 13785545
I don't have to but how do I get the object to delete after I am done with it?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13785569
>>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
 
LVL 2

Author Comment

by:jymmealey
ID: 13785608
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
 
LVL 86

Expert Comment

by:jkr
ID: 13785940
>>Do I just need to move the desctructor to a private rather than protected?

No, better move it to the 'public:' section.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 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