Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read properties of a VCL control in a non-GUI thread?

Is thread safe if a thread asynchronously read the information from VCL Controls in Delphi?

eg.

procedure TMyThread.Execute;
var bOK:Boolean; 
    iOK:Integer;
begin
   while not terminated do
   begin
      bOk:=MyForm.cbCheckBox.Checked;
      iOK:=MyForm.Left;
      sleep(20);
   end;
end;

If it is not thread safe how should I do to catch the event when the checkbox has changed its property.

like image 344
user558126 Avatar asked Jan 06 '13 10:01

user558126


1 Answers

No it is not safe. Your code is liable to lead to the window handle being created with affinity to the wrong thread.

Don't use the GUI to store your applications state. Use the GUI to show a view onto that state. Once you separate the state from the view you are home and dry. Your worker threads can use the underlying state state without touching GUI.

like image 67
David Heffernan Avatar answered Oct 17 '22 09:10

David Heffernan