[DUG] AnonymousThread question

Robert Martin rob at chreos.co.nz
Wed Jun 26 08:42:08 NZST 2013


Hi

Doing some code (which I have yet to run) that uses an AnonymousThread 
to deal with triggering some processing in the main thread without the 
calling Thread having to wait.  This is the first time if have used an 
AnonymousThread in this scenario.


I have something like

TMyThread .Execute

begin
     ..
     loop
     ....

     //Call this message from ANOTHER thread so that this thread doesn't 
need to sit and wait for
     //whatever processing occurs in the triggered event
     AnonymousThread := TThread.CreateAnonymousThread(
             procedure()
             begin
                 TThread.Synchronize(TThread.CurrentThread,
                         procedure()
                         begin
                             TCPMessage.ResultProcedure(MsgHeader, MsgBody);
                         end);
             end
             );

     AnonymousThread.Start;


     TCPMessage.Free;

     ...

end;


If TCPMessage is freed straight after initiating the AnonymousThread and 
MsgBody and MsgHeader are vars in the TMyThread  that will be changed. 
Does the AnonymousThread take a copy of the values when created 
(therefore my code is safe) or not?  If not what would be the right way 
to deal with this sort of scenario?

p.s. ResultProcedure is a MethodPointer to a method in the Main thread.


Thanks
Rob



More information about the Delphi mailing list