Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2008 Mar : How to free a thread properly

www.cryer.info
Managed Newsgroup Archive

How to free a thread properly

Subject:How to free a thread properly
Posted by:"Philippe Bruno" (philippe.bru..@mail.mcgill.ca)
Date:Sat, 8 Mar 2008 20:33:56

Hi,

I see that some programmers free a previously created thread (when
FreeOnTerminate is False) as follows:

  if Assigned(MyThread) then
  begin
    MyThread.Terminate;
    MyThread.WaitFor;
    FreeAndNil(MyThread);
  end;

However, the documentation on the Destroy() method for TThread says that
"Destroy signals the thread to terminate and then waits for the thread to
return before calling the inherited Destroy method." A quick look at the
source code confirms this statement. Is it therefore necessary to free a
thread as illustrated above or a simple call to Free as in the following
should do the trick:

  MyThread.Free;

Thanks in advance for shedding some light on that.

Philippe

Replies:

www.cryer.info
Managed Newsgroup Archive