在 Delphi 中,可以通過以下幾種方式傳遞參數給線程:
type
TMyThread = class(TThread)
private
FParam: Integer;
protected
procedure Execute; override;
public
constructor Create(Param: Integer);
end;
constructor TMyThread.Create(Param: Integer);
begin
inherited Create(True);
FParam := Param;
end;
procedure TMyThread.Execute;
begin
// 使用 FParam 進行操作
end;
然后,可以通過以下方式創建并運行線程:
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(123);
MyThread.Start;
end;
type
TMyThread = class(TThread)
private
FParam: Integer;
protected
procedure Execute; override;
public
property Param: Integer read FParam write FParam;
end;
procedure TMyThread.Execute;
begin
// 使用 FParam 進行操作
end;
然后,可以通過以下方式創建并運行線程:
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(True);
MyThread.Param := 123;
MyThread.Start;
end;
type
TMyThread = class(TThread)
private
FParam: Integer;
protected
procedure Execute; override;
public
constructor Create(Param: Integer);
end;
constructor TMyThread.Create(Param: Integer);
begin
inherited Create(True);
FParam := Param;
end;
procedure TMyThread.Execute;
begin
// 使用 FParam 進行操作
end;
然后,可以通過以下方式創建并運行線程:
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(123);
MyThread.Start;
end;
以上是幾種常見的傳遞參數給 Delphi 線程的方式,可以根據具體的需求選擇適合的方式。