1 | unit UJobProgressView;
|
---|
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 | TMethod = procedure of object;
|
---|
11 |
|
---|
12 | TJobThread = class(TThread)
|
---|
13 | procedure Execute; override;
|
---|
14 | public
|
---|
15 | ExceptionText: string;
|
---|
16 | Index: Integer;
|
---|
17 | Title: string;
|
---|
18 | Method: TMethod;
|
---|
19 | end;
|
---|
20 |
|
---|
21 | TJobProgressView = class(TForm)
|
---|
22 | ProgressBar1: TProgressBar;
|
---|
23 | Label1: TLabel;
|
---|
24 | ListView1: TListView;
|
---|
25 | ImageList1: TImageList;
|
---|
26 | Label2: TLabel;
|
---|
27 | Timer1: TTimer;
|
---|
28 | Label3: TLabel;
|
---|
29 | Animate1: TAnimate;
|
---|
30 | procedure Timer1Timer(Sender: TObject);
|
---|
31 | procedure FormCreate(Sender: TObject);
|
---|
32 | procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
---|
33 | private
|
---|
34 | FProgress: Real;
|
---|
35 | FMaxPosition: Real;
|
---|
36 | Jobs: array of record
|
---|
37 | Method: TMethod;
|
---|
38 | Direct: Boolean;
|
---|
39 | end;
|
---|
40 | Job: TJobThread;
|
---|
41 | WindowList: Pointer;
|
---|
42 | StartTime: TDateTime;
|
---|
43 | procedure SetPosition(Value: Real);
|
---|
44 | public
|
---|
45 | Terminate: Boolean;
|
---|
46 | procedure AddJob(Title: string; Method: TMethod; Direct: Boolean = False);
|
---|
47 | procedure Start;
|
---|
48 | procedure Stop;
|
---|
49 | procedure TermSleep(Delay: Integer);
|
---|
50 | property Progress: Real read FProgress write SetPosition;
|
---|
51 | property Max: Real read FMaxPosition write FMaxPosition;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | var
|
---|
55 | JobProgressView: TJobProgressView;
|
---|
56 |
|
---|
57 | implementation
|
---|
58 |
|
---|
59 | {$R *.dfm}
|
---|
60 |
|
---|
61 | procedure TJobThread.Execute;
|
---|
62 | begin
|
---|
63 | ExceptionText := '';
|
---|
64 | try
|
---|
65 | //raise Exception.Create('dsds');
|
---|
66 | Method;
|
---|
67 | Terminate;
|
---|
68 | except
|
---|
69 | on E:Exception do begin
|
---|
70 | ExceptionText := 'V úloze "' + Title + '" došlo k vyjímce: ' + E.Message;
|
---|
71 | Terminate;
|
---|
72 | end;
|
---|
73 | end;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | procedure TJobProgressView.SetPosition(Value: Real);
|
---|
77 | begin
|
---|
78 | if (Value < 0) then Value := 0;
|
---|
79 | if (Value > FMaxPosition) then Value := FMaxPosition;
|
---|
80 | FProgress := Value;
|
---|
81 | if not ProgressBar1.Visible then ProgressBar1.Show;
|
---|
82 | ProgressBar1.Position := Round(Value / FMaxPosition * 100);
|
---|
83 | Application.ProcessMessages;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TJobProgressView.AddJob(Title: string; Method: TMethod; Direct: Boolean = False);
|
---|
87 | var
|
---|
88 | NewItem: TListItem;
|
---|
89 | begin
|
---|
90 | with ListView1, Items do begin
|
---|
91 | BeginUpdate;
|
---|
92 | NewItem := Add;
|
---|
93 | with NewItem do begin
|
---|
94 | Caption := Title;
|
---|
95 | ImageIndex := 2;
|
---|
96 | end;
|
---|
97 | SetLength(Jobs, Length(Jobs) + 1);
|
---|
98 | Jobs[High(Jobs)].Method := Method;
|
---|
99 | Jobs[High(Jobs)].Direct := Direct;
|
---|
100 | EndUpdate;
|
---|
101 | end;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TJobProgressView.Start;
|
---|
105 | var
|
---|
106 | I: Integer;
|
---|
107 | begin
|
---|
108 | Caption := 'Prosím čekejte...';
|
---|
109 | try
|
---|
110 | Terminate := False;
|
---|
111 | WindowList := DisableTaskWindows(0);
|
---|
112 | Height := 100 + 18 * ListView1.Items.Count;
|
---|
113 | Show;
|
---|
114 | Timer1.Enabled := True;
|
---|
115 | // Timer1Timer(Self);
|
---|
116 | for I := 0 to High(Jobs) do begin
|
---|
117 | StartTime := Now;
|
---|
118 | ListView1.Items.Item[I].ImageIndex := 1;
|
---|
119 | Label3.Caption := '';
|
---|
120 | ProgressBar1.Position := 0;
|
---|
121 | Application.ProcessMessages;
|
---|
122 | if Jobs[I].Direct then Jobs[I].Method else begin
|
---|
123 | Job := TJobThread.Create(True);
|
---|
124 | with Job do begin
|
---|
125 | Method := Jobs[I].Method;
|
---|
126 | Title := ListView1.Items.Item[I].Caption;
|
---|
127 | Index := I;
|
---|
128 | Resume;
|
---|
129 | while not Terminated do begin
|
---|
130 | Application.ProcessMessages;
|
---|
131 | Sleep(10);
|
---|
132 | end;
|
---|
133 | WaitFor;
|
---|
134 | if ExceptionText <> '' then raise Exception.Create(ExceptionText);
|
---|
135 | Free;
|
---|
136 | end;
|
---|
137 | end;
|
---|
138 | ProgressBar1.Hide;
|
---|
139 | ListView1.Items.Item[I].ImageIndex := 0;
|
---|
140 | if Terminate then Break;
|
---|
141 | end;
|
---|
142 | finally
|
---|
143 | Timer1.Enabled := False;
|
---|
144 | EnableTaskWindows(WindowList);
|
---|
145 | if Visible then begin
|
---|
146 | JobProgressView.Hide;
|
---|
147 | end;
|
---|
148 | SetLength(Jobs, 0);
|
---|
149 | with ListView1.Items do begin
|
---|
150 | BeginUpdate;
|
---|
151 | Clear;
|
---|
152 | EndUpdate;
|
---|
153 | end;
|
---|
154 | end;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TJobProgressView.Timer1Timer(Sender: TObject);
|
---|
158 | begin
|
---|
159 | if not Visible then begin
|
---|
160 | Show;
|
---|
161 | end;
|
---|
162 | if ProgressBar1.Position > 4 then
|
---|
163 | Label3.Caption := TimeToStr((Now - StartTime) / FProgress * (1 - FProgress));
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TJobProgressView.FormCreate(Sender: TObject);
|
---|
167 | begin
|
---|
168 | Animate1.FileName := ExtractFileDir(Application.ExeName) + '\horse.avi';
|
---|
169 | Animate1.Active := True;
|
---|
170 | FMaxPosition := 1;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | procedure TJobProgressView.Stop;
|
---|
174 | begin
|
---|
175 | Terminate := True;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TJobProgressView.TermSleep(Delay: Integer);
|
---|
179 | const
|
---|
180 | Quantum = 100;
|
---|
181 | var
|
---|
182 | I: Integer;
|
---|
183 | begin
|
---|
184 | Sleep(Delay mod Quantum);
|
---|
185 | for I := 1 to (Delay div Quantum) do begin
|
---|
186 | if Terminate then Break;
|
---|
187 | Sleep(Quantum);
|
---|
188 | end;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | procedure TJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
|
---|
192 | begin
|
---|
193 | CanClose := Terminate;
|
---|
194 | Terminate := True;
|
---|
195 | Caption := 'Prosím čekejte...přerušení';
|
---|
196 | end;
|
---|
197 |
|
---|
198 | end.
|
---|