title: Thread-safe in delphi
body: <p>I have to modify and change some visual components in a thread and as you know it's not safe to doing this.</p>
  
  <p>My question is <strong>how to write a completely thread-safe code?</strong> It is possible? if it is then can you please give me a simple example?</p>
  
  <p>my code that is not threadsafe:</p>
  
  <pre><code>type
    tMyWorkerThread = class(TThread)
        public
           procedure Execute; override;
    end;
  
  var
    Form1: TForm1;
  
  implementation
  
  {$R *.dfm}
  
  procedure tMyWorkerThread.Execute;
  begin
    //codes
    //working with visual components
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  begin
    TMyWorkerThread.Create(false);
  end;
  </code></pre>
  
  <p>Thank you.</p>