传递值以同步线程

Oue*_*ine 8 delphi multithreading

我是新手,所以我已经尝试了几个小时(我正在使用XE4),

我有一个简单的线程

type
  TSendThread = class(TThread)
  private
  public
    procedure proc(const s : string);
  protected
    procedure Execute; override;
  end;

  procedure TSendThread.proc(const S: String);
  begin
    showmessage(s);
  end;
Run Code Online (Sandbox Code Playgroud)

现在,在我的主要表单中,我想用"proc"来表示:

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
begin
    t := TSendThread.create(true);
    t.Synchronize(nil, t.proc('foo'));
end;
Run Code Online (Sandbox Code Playgroud)

但每当我尝试编译,我得到:

没有可以使用这些参数调用的"Synchronize"的重载版本

这对我来说没有意义,因为当我从"proc"中删除"S"参数时,它工作正常.

Rem*_*eau 16

看看各种声明TThread.Synchronize().您正在尝试调用带有TThreadMethod输入的版本. TThreadMethod是无参数:

TThreadMethod = procedure of object;
Run Code Online (Sandbox Code Playgroud)

这就是为什么传球只是t.Proc有效,但传球t.Proc('foo')没有.

话虽如此,你完全是在滥用TThread.Synchronize().您无需创建TThread对象即可使用静态版本Synchronize().如果你确实创建了一个TThread对象,那就让它实际做一些事情,比如:

type
  TSendThread = class(TThread)
  public
    fStr: String;
    procedure DoProc;
    procedure Proc(const S: string);
  protected
    procedure Execute; override;
  end;

  procedure TSendThread.Execute;
  begin
    Proc('foo');
  end;

  procedure TSendThread.Proc(const S: string);
  begin
    fStr := S;
    Synchronize(DoProc);
  end;

  procedure TSendThread.DoProc;
  begin
    ShowMessage(fStr);
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
begin
  t := TSendThread.Create(False);
  t.WaitFor;
  t.Free;
end;
Run Code Online (Sandbox Code Playgroud)

但是,因为您正在使用XE4,所以Synchronize()也支持匿名过程,这将TSendThread在本示例中完全消除您的类,例如:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TThread.Synchronize(nil,
    procedure
    begin
      ShowMessage('foo');
    end
  );
end;
Run Code Online (Sandbox Code Playgroud)

更新:给出关于你真正想要用你的线程做什么的新信息,你需要像这样做:

type
  TSendThread = class(TThread)
  private
    fURL, fMethod, fParam: string;
  protected
    procedure Execute; override;
  public
    constructor Create(aURL, aMethod, aParam: string);
  end;

constructor TSendThread.Create(aURL, aMethod, aParam: string);
begin
  inherited Create(False);
  fURL := aUrl;
  fMethod := aMethod;
  fParam := aParam;
end;

procedure TSendThread.Execute;
begin
  // use fURL, fMethod, and fParam as needed...
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
begin
  t := TSendThread.Create('url', 'method', 'param');
  ...
end;
Run Code Online (Sandbox Code Playgroud)

或者像这样:

type
  TSendThread = class(TThread)
  private
    fURL, fMethod, fParam: string;
    procedure GetValues;
  protected
    procedure Execute; override;
  end;

procedure TSendThread.GetValues;
begin
  fURL := ...;
  fMethod := ...;
  fParam := ...;
end;

procedure TSendThread.Execute;
begin
  Synchronize(GetValues);
  // use fURL, fMethod, and fParam as needed...
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
begin
  t := TSendThread.Create(False);
  ...
end;
Run Code Online (Sandbox Code Playgroud)

  • 不,我说`Synchronize()`可以在不创建线程对象的情况下使用.那是因为`Synchronize()`有几个重载版本,其中几个被声明为`static`. (2认同)