1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SynEdit, StdCtrls, ComCtrls, AbComCtrls;
type
TProgressThread = class(TThread)
private
CurrentPercentDone: Integer;
procedure UpdateForm;
protected
procedure Execute; override;
end;
TForm1 = class(TForm)
AbProgressBar1: TAbProgressBar;
btn1: TButton;
syndt1: TSynEdit;
procedure btn1Click(Sender: TObject);
procedure OnThreadFini(sender: TObject);
private
MyThread: TProgressThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TProgressThread }
procedure TProgressThread.Execute;
begin
CurrentPercentDone := 0;
while (not Terminated) and (CurrentPercentDone < 100) do
begin
Inc(CurrentPercentDone);
Synchronize(Self, UpdateForm);
Sleep(300);
end;
CurrentPercentDone := 0;
Synchronize(Self, UpdateForm);
end;
procedure TProgressThread.UpdateForm;
begin
(Application.MainForm as TForm1).AbProgressBar1.Position := CurrentPercentDone;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
if btn1.Tag = 0 then
begin
MyThread := TProgressThread.Create(True);
MyThread.FreeOnTerminate := False;
MyThread.OnTerminate := OnThreadFini;
MyThread.Resume;
btn1.Tag := 1;
btn1.Caption := 'Stopper';
end
else
begin
MyThread.Suspend;
if MessageBox(Self.Handle, PChar('Annuler l''opération ?'), PChar('Stop it ?'), MB_YESNO) = ID_YES then
begin
MyThread.Terminate;
MyThread.Free;
btn1.Tag := 0;
btn1.Caption := 'Lancer';
end
else
MyThread.Resume;
end;
end;
procedure TForm1.OnThreadFini(sender: TObject);
begin
btn1.Tag := 0;
btn1.Caption := 'Lancer';
MyThread.Free;
end;
end. |
Partager