Thread-safe in delphi
delphi, delphi-2010, multithreading, thread-safety
Solution
My problem solved with `Synchronize!`
type
tMyWorkerThread = class(TThread)
public
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tMyWorkerThread.Execute;
begin
//codes that takes long time
Synchronize(procedure begin
//working with visual components
end
);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyWorkerThread.Create(false);
end;
Thank you all for helping me.
Problem
I have to modify and change some visual components in a thread and as you know it's not safe to doing this. My question is how to write a completely thread-safe code? It is possible? if it is then can you please give me a simple example? my code that is not threadsafe: ``` 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; ``` Thank you.