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 になり、false に設定して進行状況ダイアログを閉じるべきではありませんAContinue
か? ShellExecAndWaitTask
代わりに、タスクが終了しても常に true のままです! なんで?
編集:
David のアドバイスに従って、コードを変更しました。今は動作しますが、スレッドセーフですか?
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;