-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSystem.LogEvents.Progress.pas
More file actions
779 lines (711 loc) · 20.2 KB
/
System.LogEvents.Progress.pas
File metadata and controls
779 lines (711 loc) · 20.2 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
{ *************************************************************************** }
{ }
{ }
{ Copyright (C) Amarildo Lacerda }
{ }
{ https://github.com/amarildolacerda }
{ }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ *************************************************************************** }
{
19/07/2016 + incluido para limpara da lista itens concluidos... para liberar recursos
+ incluido metodo clear... que limpa a lista.
+
12/07/2016 * trocado TTask por TTHread para compatibilidade com versões anteriores do XE - Amarildo Lacerda
}
unit System.LogEvents.Progress;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, System.SyncObjs,
System.Generics.Collections,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ValEdit, Vcl.ExtCtrls,
Vcl.Buttons, Vcl.StdCtrls, Vcl.ComCtrls, System.Diagnostics;
type
TLogEventType = (etCreating, etWaiting, etStarting, etPreparing, etPrepared,
etLoading, etCalc, etWorking, etSaving, etEnding, etFinished, etCanceled,
etAllFinished);
const
TLogEventTypeNames: array [0 .. 12] of string = ('0%', 'na fila', 'iniciando',
'preparando', 'pronto', 'carregando', 'calculando...', 'executando',
'guardando', 'quase lá', '100%', 'Cancelado', 'Encerrado');
type
TLogListEvent = procedure(sender: TObject; msg: string) of object;
TLogProgressEvent = procedure(sender: TObject; ATipo: TLogEventType;
msg: string; APosition: double) of object;
IProgressEvents = interface
['{291F6DA5-5606-412B-9CD6-76A4E25A9C95}']
procedure SetCanCancel(const Value: Boolean);
function GetCanCancel: Boolean;
procedure SetText(const Value: string);
function GetText: string;
procedure SetMaxThreads(const Value: Integer);
function GetMaxThreads: Integer;
procedure SetMax(const Value: Integer);
function GetMax: Integer;
procedure Execute(const ACaption: String; AIdentProcess: Integer = 0);
procedure WaitFor(const ASleep: Integer = 100);
procedure WaitForAll(const ASleep: Integer = 100);
procedure SetTimeout(const Value: Integer);
procedure SetMaxFinished(const Value: Integer);
function GetMaxFinished: Integer;
function GetTimeout: Integer;
function Count: Integer;
procedure Add(sender: TObject; AMsg: string; proc: TProc<TObject>);
overload;
procedure Add(AIndex: Integer; AMsg: string; proc: TProc<Integer>);
overload;
procedure Add(AValue: string; AMsg: string; proc: TProc<string>); overload;
procedure Add(AMsg: string; proc: TProc); overload;
function Terminated: Boolean;
procedure DoProgress(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
procedure DoWorking(sender: TObject; AMsg: string);
procedure DoAllFinished(sender: TObject);
procedure Restart;
procedure Close;
procedure Clear;
property Text: string read GetText write SetText;
property CanCancel: Boolean read GetCanCancel write SetCanCancel;
property Max: Integer read GetMax write SetMax;
property MaxThreads: Integer read GetMaxThreads write SetMaxThreads;
property Timeout: Integer read GetTimeout write SetTimeout;
property MaxFinished: Integer read GetMaxFinished write SetMaxFinished;
end;
TProgressEvents = class(TForm, IProgressEvents)
Panel1: TPanel;
Panel2: TPanel;
ValueListEditor1: TValueListEditor;
SpeedButton1: TSpeedButton;
lbPosition: TLabel;
lblErro: TLabel;
lbTimer: TLabel;
cbRolarLista: TCheckBox;
Bevel1: TBevel;
procedure FormCreate(sender: TObject);
procedure FormDestroy(sender: TObject);
procedure SpeedButton1Click(sender: TObject);
procedure FormCloseQuery(sender: TObject; var CanClose: Boolean);
procedure lblErroClick(sender: TObject);
procedure FormClose(sender: TObject; var Action: TCloseAction);
private
FStopwatch: TStopwatch;
FTerminated, FAutoDeleteFinished: Boolean;
FInited: Boolean;
FSLock: TCriticalSection;
FLocal: Integer;
FMax: Integer;
FPosition: Integer;
FMaxThreads: Integer;
FCanCancel: Boolean;
FTimeout: Integer;
FMaxFinished: Integer;
{ Private declarations }
procedure SetTerminated(const Value: Boolean);
procedure DoProgressEvent(sender: TObject; ATipo: TLogEventType;
msg: string; APosition: double);
procedure DoErro(sender: TObject; msg: string);
procedure SetMax(const Value: Integer);
function GetMax: Integer;
procedure UpdatePosition;
procedure incPos(x: Integer);
procedure CheckThreads;
procedure SetMaxThreads(const Value: Integer);
function GetMaxThreads: Integer;
procedure lock;
procedure Unlock;
procedure SetText(const Value: string);
function GetText: string;
procedure WaitForAll(const ASleep: Integer = 100);
procedure SetCanCancel(const Value: Boolean);
function GetCanCancel: Boolean;
procedure CheckMax(AInc: Integer);
procedure Run(proc: TProc);
procedure SetTimeout(const Value: Integer);
procedure SetMaxFinished(const Value: Integer);
function GetMaxFinished: Integer;
function GetTimeout: Integer;
public
{ Public declarations }
procedure Execute(const ACaption: String; AIdentProcess: Integer = 0);
property Max: Integer read GetMax write SetMax;
property MaxThreads: Integer read GetMaxThreads write SetMaxThreads;
property Timeout: Integer read GetTimeout write SetTimeout;
property MaxFinished: Integer read GetMaxFinished write SetMaxFinished;
class function new: IProgressEvents; static;
procedure DoProgress(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double = 0;
nInteracoes: Integer = 1);
procedure DoWorking(sender: TObject; AMsg: string);
procedure DoAllFinished(sender: TObject);
procedure Add(sender: TObject; AMsg: string; proc: TProc<TObject>);
overload;
procedure Add(AIndex: Integer; AMsg: string; proc: TProc<Integer>);
overload;
procedure Add(AValue, AMsg: string; proc: TProc<string>); overload;
procedure Add(AMsg: string; proc: TProc); overload;
procedure WaitFor(const ASleep: Integer = 100);
procedure Restart;
procedure Clear;
function Count: Integer;
function Terminated: Boolean;
property Text: string read GetText write SetText;
property CanCancel: Boolean read GetCanCancel write SetCanCancel;
end;
implementation
{$R *.dfm}
uses {$IF CompilerVersion>28} System.Threading, {$ENDIF} System.LogEvents;
procedure TProgressEvents.Add(AIndex: Integer; AMsg: string;
proc: TProc<Integer>);
begin
if not FInited then
Execute('');
lock;
try
DoProgressEvent(self, etCreating, AMsg, 0);
InterlockedIncrement(FLocal);
if FMaxThreads <= 0 then
begin // nao usa Thread...
DoProgressEvent(self, etStarting, AMsg, 0);
proc(AIndex);
DoProgressEvent(self, etFinished, AMsg, 0);
end
else
begin
Run(
procedure
begin
DoProgressEvent(self, etStarting, AMsg, 0);
proc(AIndex);
DoProgressEvent(self, etFinished, AMsg, 0);
end);
end;
finally
Unlock;
end;
CheckThreads;
end;
procedure TProgressEvents.Add(AValue: String; AMsg: string;
proc: TProc<String>);
begin
if not FInited then
Execute('');
lock;
try
DoProgressEvent(self, etCreating, AMsg, 0);
InterlockedIncrement(FLocal);
DoProgressEvent(self, etStarting, AMsg, 0);
if FMaxThreads <= 1 then
begin
proc(AValue);
DoProgressEvent(self, etFinished, AMsg, 0);
end
else
Run(
procedure
begin
proc(AValue);
DoProgressEvent(self, etFinished, AMsg, 0);
end);
finally
Unlock;
end;
CheckThreads;
end;
procedure TProgressEvents.Add(sender: TObject; AMsg: string;
proc: TProc<TObject>);
begin
if not FInited then
Execute('');
lock;
try
DoProgressEvent(self, etCreating, AMsg, 0);
InterlockedIncrement(FLocal);
DoProgressEvent(self, etStarting, AMsg, 0);
if FMaxThreads <= 1 then
begin
proc(sender);
DoProgressEvent(self, etFinished, AMsg, 0);
end
else
Run(
procedure
begin
proc(sender);
DoProgressEvent(self, etFinished, AMsg, 0);
end);
finally
Unlock;
end;
CheckThreads;
end;
procedure TProgressEvents.Add(AMsg: string; proc: TProc);
begin
self.Add(self, AMsg,
procedure(sender: TObject)
begin
proc;
end);
end;
procedure TProgressEvents.CheckMax(AInc: Integer);
begin
if FPosition >= FMax then
FMax := FPosition + AInc;
end;
procedure TProgressEvents.CheckThreads;
begin
if Count >= FMaxThreads then
WaitFor(FTimeout);
end;
procedure TProgressEvents.Clear;
begin
Restart;
end;
function TProgressEvents.Count: Integer;
begin
lock;
try
result := (FLocal - FPosition);
finally
Unlock;
end;
end;
procedure TProgressEvents.DoAllFinished(sender: TObject);
begin
WaitForAll(FTimeout);
try
DoProgress(sender, 0, etAllFinished, '');
SetTerminated(true);
except
end;
end;
procedure TProgressEvents.DoErro(sender: TObject; msg: string);
begin
lblErro.caption := msg;
end;
procedure TProgressEvents.DoProgress(sender: TObject; identific: Integer;
ATipo: TLogEventType; msg: string; APosition: double; nInteracoes: Integer);
begin
if Terminated then
exit;
LogEvents.DoProgress(sender, identific, ATipo, msg, APosition, nInteracoes);
end;
procedure TProgressEvents.Run(proc: TProc);
begin
{$IF CompilerVersion>28}
TTask.create(
procedure
begin
proc;
end).start;
{$ELSE}
TThread.CreateAnonymousThread(
procedure
begin
proc;
end).start;
{$ENDIF}
end;
procedure TProgressEvents.DoProgressEvent(sender: TObject; ATipo: TLogEventType;
msg: string; APosition: double);
begin
if not FInited then
begin
SpeedButton1.tag := ord(FCanCancel);
Execute('');
end;
Run(
procedure
begin
if TThread.CurrentThread.CheckTerminated then
begin
SetTerminated(true);
exit;
end;
TThread.Queue(nil,
procedure
var
i: Integer;
sFinished, sCanceled: string;
begin
try
if Terminated then
exit;
lock;
try
if ATipo = etAllFinished then
begin
SpeedButton1.Enabled := true;
SpeedButton1.tag := 0;
WaitForAll(FTimeout);
SpeedButton1.caption := 'OK';
FMaxFinished := 0;
UpdatePosition;
end
else
begin
if msg = '' then
exit;
ValueListEditor1.FindRow(msg, i);
if i < 0 then
begin
if not(ATipo in [etCreating, etWaiting]) then
exit;
// if ATipo = etFinished then
// exit; // foi retirado da lista... (item velhoo)...
CheckMax(1);
i := ValueListEditor1.InsertRow(msg, '...', true);
if cbRolarLista.checked then
ValueListEditor1.Perform(WM_VSCROLL, SB_BOTTOM, 0);
UpdatePosition;
end;
sFinished := TLogEventTypeNames[ord(etFinished)];
sCanceled := TLogEventTypeNames[ord(etCanceled)];
case ATipo of
etCanceled:
if (ValueListEditor1.Cells[1, i] <> sCanceled) and
(ValueListEditor1.Cells[1, i] <> sFinished) then
begin
ValueListEditor1.Cells[1, i] := sCanceled;
incPos(1);
end;
etFinished:
if ValueListEditor1.Cells[1, i] <> sFinished then
begin
ValueListEditor1.Cells[1, i] := sFinished;
incPos(1);
end;
else
if ValueListEditor1.Cells[1, i] <> sFinished then
ValueListEditor1.Cells[1, i] := TLogEventTypeNames
[ord(ATipo)];
end;
end;
finally
Unlock;
end;
except
end;
end);
end);
end;
procedure TProgressEvents.DoWorking(sender: TObject; AMsg: string);
begin
DoProgress(sender, 0, etWorking, AMsg);
end;
procedure TProgressEvents.Execute(const ACaption: String;
AIdentProcess: Integer = 0);
begin
if AIdentProcess > 0 then
begin
LogEvents.unregister(self);
LogEvents.register(self, DoProgressEvent, AIdentProcess);
LogEvents.register(self, DoErro, AIdentProcess);
end;
Clear;
FStopwatch := TStopwatch.StartNew;
if ACaption <> '' then
caption := ACaption;
if FCanCancel then
begin
SpeedButton1.caption := 'Cancelar';
SpeedButton1.Enabled := true;
end;
Show;
FInited := true;
end;
procedure TProgressEvents.FormClose(sender: TObject; var Action: TCloseAction);
begin
SetTerminated(true);
WaitFor(FTimeout);
Action := TCloseAction.caFree;
end;
procedure TProgressEvents.FormCloseQuery(sender: TObject;
var CanClose: Boolean);
begin
SetTerminated(true);
CanClose := true;
end;
procedure TProgressEvents.FormCreate(sender: TObject);
begin
FMaxFinished := 5;
FTimeout := 100;
FStopwatch := TStopwatch.StartNew;
FAutoDeleteFinished := true;
FMaxThreads := TThread.ProcessorCount * 2;
FTerminated := false;
ValueListEditor1.Strings.Clear;
FInited := false;
FSLock := TCriticalSection.create;
FMax := 0;
LogEvents.MostraDataHora := false;
LogEvents.register(self, DoProgressEvent, 0);
LogEvents.register(self, DoErro, 0);
SpeedButton1.Enabled := false;
end;
procedure TProgressEvents.FormDestroy(sender: TObject);
begin
LogEvents.unregister(self);
FSLock.free;
end;
function TProgressEvents.GetCanCancel: Boolean;
begin
result := FCanCancel;
end;
function TProgressEvents.GetMax: Integer;
begin
result := FMax;
end;
function TProgressEvents.GetMaxFinished: Integer;
begin
result := FMaxFinished;
end;
function TProgressEvents.GetMaxThreads: Integer;
begin
result := FMaxThreads;
end;
function TProgressEvents.GetText: string;
begin
lock;
try
result := Panel1.caption;
finally
Unlock;
end;
end;
function TProgressEvents.GetTimeout: Integer;
begin
result := FTimeout;
end;
procedure TProgressEvents.incPos(x: Integer);
begin
FPosition := FPosition + x;
UpdatePosition;
if FAutoDeleteFinished = false then
exit;
Run(
procedure
begin
TThread.Queue(nil,
procedure
var
i, n: Integer;
begin
try
if Terminated then
exit;
lock;
try
// apaga os concluidos mais velhos para liberar recursos
n := 0;
for i := ValueListEditor1.RowCount - 1 downto 0 do
begin
if Terminated then
exit;
try
if (TLogEventTypeNames[ord(etFinished)
] = ValueListEditor1.Values[ValueListEditor1.Keys[i]]) or
(TLogEventTypeNames[ord(etCanceled)
] = ValueListEditor1.Values[ValueListEditor1.Keys[i]]) then
begin
inc(n);
if n > FMaxFinished then
begin
ValueListEditor1.DeleteRow(i);
end;
end;
except
end;
end;
finally
Unlock;
end;
except
end;
end);
end);
end;
procedure TProgressEvents.lblErroClick(sender: TObject);
begin
showMessage(lblErro.caption);
end;
procedure TProgressEvents.lock;
begin
FSLock.Acquire;
end;
class function TProgressEvents.new: IProgressEvents;
begin
result := TProgressEvents.create(nil);
result.Clear;
end;
procedure TProgressEvents.Restart;
var
i: Integer;
begin
lock;
try
FStopwatch := TStopwatch.StartNew;
ValueListEditor1.Strings.Clear;
FMax := 0;
FPosition := 0;
FLocal := 0;
UpdatePosition;
finally
Unlock;
end;
end;
procedure TProgressEvents.SetCanCancel(const Value: Boolean);
begin
FCanCancel := Value;
end;
procedure TProgressEvents.SetMax(const Value: Integer);
begin
TThread.Queue(nil,
procedure
begin
lock;
try
FMax := Value;
finally
Unlock;
end;
UpdatePosition;
end);
end;
procedure TProgressEvents.SetMaxFinished(const Value: Integer);
begin
FMaxFinished := Value;
end;
procedure TProgressEvents.SetMaxThreads(const Value: Integer);
begin
FMaxThreads := Value;
end;
procedure TProgressEvents.SetTerminated(const Value: Boolean);
begin
lock;
try
FTerminated := true;
finally
Unlock;
end;
end;
procedure TProgressEvents.SetText(const Value: string);
begin
TThread.Queue(nil,
procedure
begin
lock;
try
Panel1.caption := Value;
finally
Unlock;
end;
end);
end;
procedure TProgressEvents.SetTimeout(const Value: Integer);
begin
FTimeout := Value;
end;
procedure TProgressEvents.SpeedButton1Click(sender: TObject);
begin
SetTerminated(true);
WaitForAll(FTimeout);
Close;
end;
function TProgressEvents.Terminated: Boolean;
begin
lock;
try
result := FTerminated;
if result then; // como parar a TASK
finally
Unlock;
end;
end;
procedure TProgressEvents.Unlock;
begin
FSLock.Release;
end;
procedure TProgressEvents.UpdatePosition;
begin
Run(
procedure
begin
TThread.Queue(nil,
procedure
var
t: Integer;
n: Integer;
x: double;
begin
try
if Terminated then
exit;
lock;
try
lbPosition.caption := '';
n := ValueListEditor1.Strings.Count;
t := FMax;
if (t = 0) or (t < n) then
begin
t := n;
FMax := n;
end;
lbPosition.caption := intToStr(FPosition) + ' de ' + intToStr(t);
x := FStopwatch.Elapsed.TotalDays;
lbTimer.caption := TimeToStr(x);
finally
Unlock;
end;
except
end;
end);
end);
end;
procedure TProgressEvents.WaitFor(const ASleep: Integer);
begin
try
if FMaxThreads > 0 then
while Count >= FMaxThreads do
begin
TThread.sleep(ASleep);
Application.ProcessMessages;
if Terminated then
break;
end;
except
end;
end;
procedure TProgressEvents.WaitForAll(const ASleep: Integer);
var
tick: int64;
begin
try
tick := TThread.GetTickCount;
while Count > 0 do
begin
Application.ProcessMessages;
if Terminated then
break;
if (TThread.GetTickCount - tick) > ASleep then
break;
end;
except
end;
end;
end.