为什么线程中的代码执行了两次?

Why is the code in the thread executed twice?

如题所示,为什么以下代码执行了两次(在控制台中执行了 2x Test)以及如何解决?

type
  TSelfThread = class(TThread)
    procedure Execute; override;
end;

procedure TSelfThread.Execute;
begin
  Writeln('Test');
end;

var
  SelfThread : TSelfThread;
begin
  try
    SelfThread := TSelfThread.Create(False);
  except
    on E: Exception do
      Writeln('Error');
  end;
end.

此行为的唯一可能解释是代码中的错误,您无法在终止进程之前等待线程完成。

将代码改成这样:

SelfThread := TSelfThread.Create(False);
SelfThread.WaitFor;