delphi 中的线程安全 [英] Thread-safe in delphi
本文介绍了delphi 中的线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在一个线程中修改和更改一些可视组件,正如您所知,这样做是不安全的.
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;
谢谢.
推荐答案
我的问题用 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;
感谢大家对我的帮助.
这篇关于delphi 中的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文