使用Synchronize时出现问题

时间:2011-08-11 06:22:00

标签: multithreading delphi thread-safety

我需要在一个单独的线程中执行一个函数,然后等待线程完成。

例如,这是原始函数:

Procedure Search;
begin
  CallA;
  CallB;
end;

这是修改后的功能:

Procedure Search;
var
  testMyThread: TMyThread;
  Done: Boolean;
begin
  // create a new thread to execute CallA
  testMyThread:=TMyThread.Create(False,Done);
  WaitForSingleObject(testMyThread.Handle, INFINITE );
  if not Done then
  begin
    TerminateThread(testMyThread.Handle, 0);
  end
  else;  
  CallB;
end

unit uMyThread;

interface

uses classes;

type
  TMyThread = class(TThread)
  private
    { Private declarations }
    FDone: ^boolean;
  protected
    procedure Execute; override;
  public
    constructor Create(const aSuspended: boolean; var Done: boolean);
    procedure CallA;
  end;

implementation

uses uMain;

constructor TMyThread.Create(const aSuspended: boolean;
  var Done: boolean);
begin
  inherited Create(aSuspended);
  FDone := @Done;
end;

procedure TMyThread.CallA;
begin
  // enumurating several things + updating the GUI
end;

procedure TMyThread.Execute;
begin
  inherited;
  Synchronize(CallA); // << the problem
  FDone^ := true;
end;
end.

如果我在CallA内使用Synchronize,您能告诉我为什么上面的线程代码不起作用(TMyThread.Execute永远不会被执行)吗?

2 个答案:

答案 0 :(得分:5)

因为Synchronize将在应用程序的消息循环中调用方法。使用WaitForSingleObject,您只需将所有应用程序置于保持状态。试试这个:

  Procedure Search;
  var
    testMyThread: TMyThread;
    Done: Boolean;
  begin
    // create a new thread to execute CallA
    testMyThread:=TMyThread.Create(False,Done);

    while (not Done) and (not Application.Terminated) do
      Application.ProcessMessages;

    if not Application.Terminated then
      CallB;
  end

答案 1 :(得分:1)

Delphi tthread类有一个名为onThreadTerminate的事件。 当线程离开execute方法时,在应用程序线程的上下文中调用它。

您可以在应用程序中使用此事件。