在 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
关闭进度对话框吗?相反,尽管任务已终止AContinue
,但它始终保持真实!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;