1 | unit UWaitingDialog;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, ImgList, ComCtrls, StdCtrls, ExtCtrls;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TMetoda = procedure of object;
|
---|
11 |
|
---|
12 | TWaitingTask = class(TThread)
|
---|
13 | procedure Execute; override;
|
---|
14 | public
|
---|
15 | Cislo: Integer;
|
---|
16 | Nazev: string;
|
---|
17 | Metoda: TMetoda;
|
---|
18 | end;
|
---|
19 |
|
---|
20 | TWaitingDialog = class(TForm)
|
---|
21 | ProgressBar1: TProgressBar;
|
---|
22 | Label1: TLabel;
|
---|
23 | ListView1: TListView;
|
---|
24 | ImageList1: TImageList;
|
---|
25 | Label2: TLabel;
|
---|
26 | Timer1: TTimer;
|
---|
27 | Label3: TLabel;
|
---|
28 | Animate1: TAnimate;
|
---|
29 | procedure Timer1Timer(Sender: TObject);
|
---|
30 | procedure FormCreate(Sender: TObject);
|
---|
31 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
32 | private
|
---|
33 | FPozice: Real;
|
---|
34 | Ulohy: array of record
|
---|
35 | Operace: TMetoda;
|
---|
36 | Primo: Boolean;
|
---|
37 | end;
|
---|
38 | Chyba: Integer;
|
---|
39 | Uloha: TWaitingTask;
|
---|
40 | WindowList: Pointer;
|
---|
41 | Zacatek: TDateTime;
|
---|
42 | procedure NastavPozici(NovaPozice: Real);
|
---|
43 | public
|
---|
44 | Terminate: Boolean;
|
---|
45 | property Pozice: Real write NastavPozici;
|
---|
46 | procedure PridejUlohu(Nazev: string; Metoda: TMetoda; Primo: Boolean=False);
|
---|
47 | procedure Start;
|
---|
48 | procedure Stop;
|
---|
49 | { Public declarations }
|
---|
50 | end;
|
---|
51 |
|
---|
52 | var
|
---|
53 | WaitingDialog: TWaitingDialog;
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | {$R *.dfm}
|
---|
58 |
|
---|
59 | procedure TWaitingTask.Execute;
|
---|
60 | begin
|
---|
61 | try
|
---|
62 | try
|
---|
63 | Metoda;
|
---|
64 | except
|
---|
65 | raise Exception.Create('V úloze "' + Nazev + '" došlo k vyjímce!');
|
---|
66 | end;
|
---|
67 | finally
|
---|
68 | Terminate;
|
---|
69 | end;
|
---|
70 | end;
|
---|
71 |
|
---|
72 | procedure TWaitingDialog.NastavPozici(NovaPozice: Real);
|
---|
73 | begin
|
---|
74 | if (NovaPozice<0) then NovaPozice:= 0;
|
---|
75 | if (NovaPozice>100) then NovaPozice:= 100;
|
---|
76 | if not ProgressBar1.Visible then ProgressBar1.Show;
|
---|
77 | ProgressBar1.Position:= Round(NovaPozice);
|
---|
78 | FPozice:= NovaPozice;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TWaitingDialog.PridejUlohu(Nazev: string; Metoda: TMetoda; Primo: Boolean=False);
|
---|
82 | var
|
---|
83 | Novy: TListItem;
|
---|
84 | begin
|
---|
85 | ListView1.Items.BeginUpdate;
|
---|
86 | Novy:= ListView1.Items.Add;
|
---|
87 | with Novy do begin
|
---|
88 | Caption:= Nazev;
|
---|
89 | ImageIndex:= 2;
|
---|
90 | end;
|
---|
91 | SetLength(Ulohy,Length(Ulohy)+1);
|
---|
92 | Ulohy[High(Ulohy)].Operace:= Metoda;
|
---|
93 | Ulohy[High(Ulohy)].Primo:= Primo;
|
---|
94 | ListView1.Items.EndUpdate;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TWaitingDialog.Start;
|
---|
98 | var
|
---|
99 | I: Integer;
|
---|
100 | begin
|
---|
101 | try
|
---|
102 | Terminate := False;
|
---|
103 | Chyba:= 0;
|
---|
104 | WindowList := DisableTaskWindows(0);
|
---|
105 | Height:= 90+19*ListView1.Items.Count;
|
---|
106 | Zacatek:= Now;
|
---|
107 | //Show;
|
---|
108 | Timer1.Enabled:= True;
|
---|
109 | // Timer1Timer(Self);
|
---|
110 | for I:= 0 to High(Ulohy) do begin
|
---|
111 | ListView1.Items.Item[I].ImageIndex:= 1;
|
---|
112 | Label3.Caption:= '';
|
---|
113 | ProgressBar1.Position:= 0;
|
---|
114 | if Ulohy[I].Primo then Ulohy[I].Operace else begin
|
---|
115 | Uloha := TWaitingTask.Create(True);
|
---|
116 | with Uloha do begin
|
---|
117 | Metoda:= Ulohy[I].Operace;
|
---|
118 | Nazev:= ListView1.Items.Item[I].Caption;
|
---|
119 | Cislo:= I;
|
---|
120 | Resume;
|
---|
121 | while not Terminated do Application.ProcessMessages;
|
---|
122 | WaitFor;
|
---|
123 | Free;
|
---|
124 | end;
|
---|
125 | end;
|
---|
126 | ProgressBar1.Hide;
|
---|
127 | ListView1.Items.Item[I].ImageIndex:= 0;
|
---|
128 | if Chyba>0 then Break;
|
---|
129 | end;
|
---|
130 | finally
|
---|
131 | Timer1.Enabled:= False;
|
---|
132 | EnableTaskWindows(WindowList);
|
---|
133 | if Visible then begin
|
---|
134 | WaitingDialog.Hide;
|
---|
135 | end;
|
---|
136 | SetLength(Ulohy,0);
|
---|
137 | ListView1.Items.BeginUpdate;
|
---|
138 | ListView1.Clear;
|
---|
139 | ListView1.Items.EndUpdate;
|
---|
140 | end;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TWaitingDialog.Timer1Timer(Sender: TObject);
|
---|
144 | begin
|
---|
145 | if not Visible then begin
|
---|
146 | Show;
|
---|
147 | end;
|
---|
148 | if ProgressBar1.Position>4 then
|
---|
149 | Label3.Caption := TimeToStr((Now-Zacatek)/FPozice*(100-FPozice));
|
---|
150 | end;
|
---|
151 |
|
---|
152 | procedure TWaitingDialog.FormCreate(Sender: TObject);
|
---|
153 | begin
|
---|
154 | Animate1.FileName := ExtractFileDir(Application.ExeName)+ '\horse.avi';
|
---|
155 | Animate1.Active := True;
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TWaitingDialog.Stop;
|
---|
159 | begin
|
---|
160 | Terminate := True;
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure TWaitingDialog.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
164 | begin
|
---|
165 | Terminate := True;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | end.
|
---|