6

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.

user558126
  • 1,303
  • 3
  • 21
  • 42
  • See [Can threads safely read variables set by VCL events?](http://stackoverflow.com/q/2600664/576719). – LU RD Jan 06 '13 at 10:38
  • As David say, dealing directly with the vcl is not safe. In the link above, the state of the vcl control is stored in a variable. In this case it was a boolean, and that is safe. But other types may be unsafe . – LU RD Jan 06 '13 at 10:57

1 Answers1

11

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • So I need to use the Synchronize method? – user558126 Jan 06 '13 at 10:42
  • How should I do to solve my problem? To catch the event when the Checkbox was changed? – user558126 Jan 06 '13 at 10:45
  • 2
    Well you could, but as I said, you'd be better not touching the GUI at all. Your underlying model object should hold the state. Read it from there. Don't hold your state in GUI objects. – David Heffernan Jan 06 '13 at 10:46
  • How should I do to solve my problem? To catch the event when the Checkbox was changed? – – user558126 Jan 06 '13 at 10:48
  • 1
    Solve the problem using the method described in the answer. Hold the state of your app in an object commonly know as the view. Let your GUI be a view of that model. When the user makes changes in the GUI, push those changes to the model. Then your worker threads don't need to touch the VCL and your problems go away. Read about model-view-controller. – David Heffernan Jan 06 '13 at 10:52
  • and how do I do to change the model's properties – user558126 Jan 06 '13 at 10:54
  • 1
    Call methods, assign to properties etc. Nothing special, just plain simple Delphi OOP coding. – David Heffernan Jan 06 '13 at 11:06
  • So I will do an object which will be accessible from the GUI and from the Thread. When the property will be changed, the GUI will trigger be an event and then the GUI will change the property from that common object? Can you give me a link with an example source code? – user558126 Jan 06 '13 at 11:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22260/discussion-between-user558126-and-david-heffernan) – user558126 Jan 06 '13 at 11:14
  • 1
    Why not simply send (post) a message to the thread from the gui thread when the checkbox status changes? – Remko Jan 06 '13 at 12:19