3

Since C++ Builder 2010 does not seem to include the CheckTerminated() function on TThread I added my own to a thread I needed to check the status of from outside the thread.

This works fine, but I'm trying to find information as to whether or not letting the Exectue() function of the thread finish sets the Terminated property to true or if I manually have to do that at the end of the Execute() method, alternatively run Terminate() at the end of the Execute() method.

inquam
  • 12,664
  • 15
  • 61
  • 101

1 Answers1

3

Set FreeOnTerminate to false and read Finished propery to find out if Execute() function has been finished.

if(myThread->Finished)
{
}
mh taqia
  • 3,506
  • 1
  • 24
  • 35
  • So Execute() finishing does NOT set the property, I have to manually do it at the end of the Execute() method if I want it set? – inquam Feb 11 '14 at 10:42
  • 2
    Terminated does not mean that the thread has finished - it means that Terminate() has been called. Ie, it's a flag *to* terminate, or that the thread *should be* terminated, not that it *has* terminated. So you should not set it at the end of Execute, since its only function is to provide info to code inside Execute that it should quit early. Does that explain the difference between the two? As @mhtaqia says, the correct thing to check if the thread has completed is Finished. – David Feb 11 '14 at 12:45
  • 1
    Just be careful. If `TThread.DoTerminate()` or `TThread.OnTerminate` raises an exception, `TThread.Finished` will never be set to true. A safer option would be to call [`WaitForSingleObject()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032.aspx) and see if it reports `WAIT_OBJECT_0` or not. – Remy Lebeau Feb 13 '14 at 01:14