Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a VCL component from CreateAnonymousThread

It seems which Synchronize cannot be used from a Thread created using CreateAnonymousThread, so the question is : How i can update a VCL component from inside of a Thread created using CreateAnonymousThread?

TThread.CreateAnonymousThread(procedure
 begin
  //do something
  UpdateCompnent();//how I can update a VCL component from here?   
 end
).Start;
like image 540
Salvador Avatar asked Jul 18 '11 21:07

Salvador


2 Answers

You can use synchronize in this case, e.g.:

TThread.Synchronize(nil, procedure begin UpdateComponent(); end);

And if you want asynchronous method call execution within the main thread, you can use TThread.Queue, e.g.:

TThread.Queue(nil, procedure begin UpdateComponent(); end);
like image 181
Linas Avatar answered Oct 05 '22 01:10

Linas


You could also use PostMessage to safely queue, or SendMessage to safely synchronize from an anonymous thread.

like image 41
Warren P Avatar answered Oct 05 '22 02:10

Warren P