在Delphi中,可以通過以下方法傳遞參數(shù)給線程:
type
TMyThread = class(TThread)
private
FParam: Integer;
public
constructor Create(Param: Integer);
procedure Execute; override;
end;
constructor TMyThread.Create(Param: Integer);
begin
inherited Create(True);
FParam := Param;
end;
procedure TMyThread.Execute;
begin
// 使用 FParam 做一些操作
end;
// 創(chuàng)建線程并傳遞參數(shù)
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(123);
MyThread.Start;
end;
type
TMyThread = class(TThread)
private
FParam: Integer;
public
property Param: Integer read FParam write FParam;
procedure Execute; override;
end;
procedure TMyThread.Execute;
begin
// 使用 Param 做一些操作
end;
// 創(chuàng)建線程并設(shè)置參數(shù)
var
MyThread: TMyThread;
begin
MyThread := TMyThread.Create(True);
MyThread.Param := 123;
MyThread.Start;
end;
這兩種方法都可以用來傳遞參數(shù)給線程,在線程的 Execute
方法中可以使用傳遞的參數(shù)進(jìn)行操作。