[Edit]
改进的答案,现在线程连续运行并“继续观察值”。
让我们构建一个示例。
首先,创建新的 VCL 应用程序。在窗体上放置一个 TListBox 和两个 TButton 组件。您需要编写按钮单击处理程序并添加一个私有方法。整个单元应该是这样的:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FCollector: TCollector;
procedure OnCollect(S: TStrings);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(FCollector) then Exit;
FCollector := TCollector.Create;
FCollector.OnCollect := Self.OnCollect;
FCollector.Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if not Assigned(FCollector) then Exit;
FCollector.Terminate;
FCollector := nil;
end;
procedure TForm1.OnCollect(S: TStrings);
begin
ListBox1.Items.AddStrings(S);
end;
end.
接下来我们应该添加线程,从菜单中选择:文件 -> 新单元并替换为代码:
单元2;
interface
uses
System.Classes;
type
TCollectEvent = procedure (S: TStrings) of object;
TCollector = class(TThread)
private
{ Private declarations }
FTick: THandle;
FItems: TStrings;
FOnCollect: TCollectEvent;
FInterval: Integer;
protected
procedure Execute; override;
procedure DoCollect;
public
constructor Create;
destructor Destroy; override;
procedure Terminate;
property Interval: Integer read FInterval write FInterval;
property OnCollect: TCollectEvent read FOnCollect write FOnCollect;
end;
implementation
uses Windows, SysUtils;
{ TCollector }
constructor TCollector.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
FInterval := 1000;
FTick := CreateEvent(nil, True, False, nil);
end;
destructor TCollector.Destroy;
begin
CloseHandle(FTick);
inherited;
end;
procedure TCollector.DoCollect;
begin
FOnCollect(FItems);
end;
procedure TCollector.Terminate;
begin
inherited;
SetEvent(FTick);
end;
procedure TCollector.Execute;
begin
while not Terminated do
begin
if WaitForSingleObject(FTick, FInterval) = WAIT_TIMEOUT then
begin
FItems := TStringList.Create;
try
// Collect here items
FItems.Add('Item ' + IntToStr(Random(100)));
Synchronize(DoCollect);
finally
FItems.Free;
end;
end;
end;
end;
end.
现在,当您按下 Button1 时,您将从组合中的线程接收项目。按下 Button2 线程将停止执行。
您应该考虑:
设置Interval为观察值,默认为1000ms,参见interval属性;
Execute 中及其相关的所有代码(包括数据库访问组件)都应该是线程保存的。