如何从外部检查ITask线程是否仍在运行?

时间:2015-04-22 12:49:28

标签: multithreading delphi delphi-xe7 delphi-xe8

在Delphi XE7(或XE8)中,在表单上放置 TjvProgressDialog (来自JVCL),并将其命名为 dlgProgress1 。还有一个TButton,并将其命名为 btnProgressDialogTest

此代码首先从单独的线程(ShellExecAndWaitTask)启动记事本,然后打开具有无限进度循环的进度对话框(dlgProgress1):

var
  ShellExecAndWaitTask: System.Threading.ITask;

procedure TForm1.btnProgressDialogTestClick(Sender: TObject);
begin
  ShellExecAndWaitTask := TTask.Create(
    procedure
    begin
      JclShell.ShellExecAndWait('notepad'); // BTW, is this thread-safe?
      CodeSite.Send('Notepad has been closed');
    end);
  ShellExecAndWaitTask.Start;

  dlgProgress1.Caption := 'ProgressDialog Test';
  dlgProgress1.Text := 'Close Notepad to automatically close this progress dialog';
  dlgProgress1.Tag := 0;
  dlgProgress1.Position := 0;
  dlgProgress1.ShowCancel := True;
  dlgProgress1.ShowModal;
  CodeSite.Send('Progress dialog has been closed');
end;

procedure TForm1.dlgProgress1Progress(Sender: TObject; var AContinue: Boolean);
begin
  if dlgProgress1.Tag = 0 then
  begin
    if dlgProgress1.Position < dlgProgress1.Max then
      dlgProgress1.Position := dlgProgress1.Position + 1;
    if dlgProgress1.Position = dlgProgress1.Max then
      dlgProgress1.Tag := 1;
  end
  else
  begin
    if dlgProgress1.Position > 0 then
      dlgProgress1.Position := dlgProgress1.Position - 1;
    if dlgProgress1.Position = 0 then
      dlgProgress1.Tag := 0;
  end;

  AContinue := Assigned(ShellExecAndWaitTask); // why this never becomes false?
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Assigned(ShellExecAndWaitTask) then
    ShellExecAndWaitTask.Cancel;
end;

当您关闭记事本时,Assigned(ShellExecAndWaitTask)事件处理程序中的dlgProgress1Progress不应变为false并通过将AContinue设置为false来关闭进度对话框?相反,尽管ShellExecAndWaitTask任务已经终止,但它始终保持为真!为什么呢?

修改

按照大卫的建议,我改变了代码。现在它可以工作,但是它是线程安全的吗?

var
  ShellExecAndWaitTask: System.Threading.ITask;
  ShellExecAndWaitTaskTerminated: Boolean;

procedure TForm1.btnProgressDialogTestClick(Sender: TObject);
begin
  ShellExecAndWaitTask := TTask.Create(
    procedure
    begin
      JclShell.ShellExecAndWait('notepad'); // BTW, is this thread-safe?
      CodeSite.Send('Notepad has been closed');
      TThread.Queue(TThread.CurrentThread,
      procedure
      begin
        ShellExecAndWaitTaskTerminated := True;
      end);
    end);
  ShellExecAndWaitTaskTerminated := False;
  ShellExecAndWaitTask.Start;

  dlgProgress1.Caption := 'ProgressDialog Test';
  dlgProgress1.Text := 'Close Notepad to automatically close this progress dialog';
  dlgProgress1.Tag := 0;
  dlgProgress1.Position := 0;
  dlgProgress1.ShowCancel := True;
  dlgProgress1.ShowModal;
  CodeSite.Send('Progress dialog has been closed');
end;

procedure TForm1.dlgProgress1Progress(Sender: TObject; var AContinue: Boolean);
begin
  if dlgProgress1.Tag = 0 then
  begin
    if dlgProgress1.Position < dlgProgress1.Max then
      dlgProgress1.Position := dlgProgress1.Position + 1;
    if dlgProgress1.Position = dlgProgress1.Max then
      dlgProgress1.Tag := 1;
  end
  else
  begin
    if dlgProgress1.Position > 0 then
      dlgProgress1.Position := dlgProgress1.Position - 1;
    if dlgProgress1.Position = 0 then
      dlgProgress1.Tag := 0;
  end;
  AContinue := not ShellExecAndWaitTaskTerminated;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Assigned(ShellExecAndWaitTask) then
    ShellExecAndWaitTask.Cancel;
end;

1 个答案:

答案 0 :(得分:1)

  

当您关闭记事本时,dlgProgress1Progress事件处理程序中的Assigned(ShellExecAndWaitTask)是否会变为false并通过将AContinue设置为false来关闭进度对话框?

不,事情根本不可行。例如,您可以使用许多不同的变量来引用该任务。您怎么能期望所有这些变量都设置为nil

不,接口引用变量在保留范围之前保持分配状态,或者将其设置为nil。顺便说一下,我严肃地质疑你选择使用全局变量。我不知道你做出这个决定的原因,但这似乎是错误的决定。

您最简单的方法是在致电ShellExecAndWait时发送消息。

ShellExecAndWaitTask := TTask.Create(
  procedure
  begin
    JclShell.ShellExecAndWait('notepad');
    CodeSite.Send('Notepad has been closed');
    // notify the main thread that the external process has completed
  end);

您可以使用PostMessageTThread.QueueTThread.Synchronize来通知主线程。