/
BackgroundWorker.pas
executable file
·299 lines (256 loc) · 9.12 KB
/
BackgroundWorker.pas
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
{************************************************************************
Copyright 2015 Oliver Münzberg (aka Sir Rufo)
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.
************************************************************************}
unit BackgroundWorker;
interface
uses
System.Classes,
System.Rtti,
System.SysUtils;
{$REGION 'EventArgs'}
type
TProgressChangedEventArgs = class
private
FPercentProgress: Integer;
FUserState : TValue;
public
constructor Create( APercentProgress: Integer; AUserState: TValue );
property PercentProgress: Integer read FPercentProgress;
property UserState: TValue read FUserState;
end;
TDoWorkEventArgs = class
private
FArgument: TValue;
FCancel : Boolean;
FResult : TValue;
public
constructor Create( AArgument: TValue );
property Argument: TValue read FArgument;
property Cancel: Boolean read FCancel write FCancel;
property Result: TValue read FResult write FResult;
end;
TRunWorkerCompletedEventArgs = class
private
FCancelled: Boolean;
FError : Exception;
FResult : TValue;
public
constructor Create( AResult: TValue; AError: Exception; ACancelled: Boolean );
property Cancelled: Boolean read FCancelled;
property Error: Exception read FError;
property Result: TValue read FResult;
end;
{$ENDREGION}
{$REGION 'Events'}
type
TBackgroundWorkerProgressChangedEvent = procedure( Sender: TObject; e: TProgressChangedEventArgs ) of object;
TBackgroundWorkerDoWorkEvent = procedure( Sender: TObject; e: TDoWorkEventArgs ) of object;
TBackgroundWorkerRunWorkerCompletedEvent = procedure( Sender: TObject; e: TRunWorkerCompletedEventArgs ) of object;
{$ENDREGION}
{$REGION 'CustomBackgroundWorker'}
type
TCustomBackgroundWorker = class( TComponent )
private
FThread : TThread;
FDoWorkEventArg : TDoWorkEventArgs;
FCancellationPending : Boolean;
FWorkerReportsProgress : Boolean;
FWorkerSupportsCancellation: Boolean;
FOnDoWork : TBackgroundWorkerDoWorkEvent;
FOnProgressChanged : TBackgroundWorkerProgressChangedEvent;
FOnRunWorkerCompleted : TBackgroundWorkerRunWorkerCompletedEvent;
function GetCancellationPending: Boolean;
procedure WorkerThreadTerminate( Sender: TObject );
function GetIsBusy: Boolean;
protected
procedure NotifyDoWork( e: TDoWorkEventArgs ); virtual;
procedure NotifyProgressChanged( e: TProgressChangedEventArgs; ADispose: Boolean = True ); virtual;
procedure NotifyRunCompleted( e: TRunWorkerCompletedEventArgs; ADispose: Boolean = True ); virtual;
public
procedure CancelAsync;
procedure ReportProgress( PercentProgress: Integer ); overload;
procedure ReportProgress( PercentProgress: Integer; UserState: TValue ); overload;
procedure RunWorkerAsync; overload;
procedure RunWorkerAsync<T>( Argument: T ); overload;
procedure RunWorkerAsync( Argument: TValue ); overload;
property CancellationPending: Boolean read GetCancellationPending;
property IsBusy: Boolean read GetIsBusy;
protected
property OnDoWork : TBackgroundWorkerDoWorkEvent read FOnDoWork write FOnDoWork;
property OnProgressChanged : TBackgroundWorkerProgressChangedEvent read FOnProgressChanged write FOnProgressChanged;
property OnRunWorkerCompleted: TBackgroundWorkerRunWorkerCompletedEvent read FOnRunWorkerCompleted write FOnRunWorkerCompleted;
public
property WorkerReportsProgress : Boolean read FWorkerReportsProgress write FWorkerReportsProgress;
property WorkerSupportsCancellation: Boolean read FWorkerSupportsCancellation write FWorkerSupportsCancellation;
end;
{$ENDREGION}
{$REGION 'TBackgroundWorker'}
type
TBackgroundWorker = class( TCustomBackgroundWorker )
published
property OnDoWork;
property OnProgressChanged;
property OnRunWorkerCompleted;
property WorkerReportsProgress default False;
property WorkerSupportsCancellation default False;
end;
{$ENDREGION}
implementation
{$REGION 'Ressourcestrings'}
resourcestring
SIntegerArgumentOutOfRange = '%s: value %d is out of range [%d..%d]';
SWorkerDoesNotSupportsCancellation = 'Worker does not supports cancellation';
SWorkerDoesNotReportsProgress = 'Worker does not reports progress';
SWorkerIsBusy = 'Worker is busy';
{$ENDREGION}
{$REGION 'EventArgs'}
{ TProgressChangedEventArgs }
constructor TProgressChangedEventArgs.Create( APercentProgress: Integer; AUserState: TValue );
begin
inherited Create;
if ( APercentProgress < 0 ) or ( APercentProgress > 100 )
then
raise EArgumentOutOfRangeException.CreateFmt( SIntegerArgumentOutOfRange, [ 'APercentProgress', APercentProgress, 0, 100 ] );
FPercentProgress := APercentProgress;
FUserState := AUserState;
end;
{ TDoWorkEventArgs }
constructor TDoWorkEventArgs.Create( AArgument: TValue );
begin
inherited Create;
FArgument := AArgument;
end;
{ TRunWorkerCompletedEventArgs }
constructor TRunWorkerCompletedEventArgs.Create( AResult: TValue; AError: Exception; ACancelled: Boolean );
begin
inherited Create;
FCancelled := ACancelled;
FError := AError;
FResult := AResult;
end;
{$ENDREGION}
{$REGION 'TCustomBackgroundWorker'}
{ TCustomBackgroundWorker }
procedure TCustomBackgroundWorker.CancelAsync;
begin
if not WorkerSupportsCancellation
then
raise EInvalidOpException.Create( SWorkerDoesNotSupportsCancellation );
FCancellationPending := True;
end;
procedure TCustomBackgroundWorker.ReportProgress( PercentProgress: Integer );
begin
ReportProgress( PercentProgress, TValue.Empty );
end;
function TCustomBackgroundWorker.GetCancellationPending: Boolean;
begin
Result := ( csDestroying in ComponentState ) or FCancellationPending;
end;
function TCustomBackgroundWorker.GetIsBusy: Boolean;
begin
Result := Assigned( FThread );
end;
procedure TCustomBackgroundWorker.NotifyDoWork( e: TDoWorkEventArgs );
begin
if Assigned( FOnDoWork )
then
FOnDoWork( Self, e );
end;
procedure TCustomBackgroundWorker.NotifyProgressChanged( e: TProgressChangedEventArgs; ADispose: Boolean );
begin
if not( csDestroying in ComponentState )
then
TThread.Queue( nil,
procedure
begin
try
if Assigned( FOnProgressChanged )
then
FOnProgressChanged( Self, e );
finally
if ADispose
then
e.Free;
end;
end )
else
begin
if ADispose
then
e.Free;
end;
end;
procedure TCustomBackgroundWorker.NotifyRunCompleted( e: TRunWorkerCompletedEventArgs; ADispose: Boolean );
begin
try
if not( csDestroying in ComponentState )
then
if Assigned( FOnRunWorkerCompleted )
then
FOnRunWorkerCompleted( Self, e );
finally
if ADispose
then
e.Free;
end;
end;
procedure TCustomBackgroundWorker.ReportProgress( PercentProgress: Integer; UserState: TValue );
begin
if not WorkerReportsProgress
then
raise EInvalidOpException.Create( SWorkerDoesNotReportsProgress );
NotifyProgressChanged( TProgressChangedEventArgs.Create( PercentProgress, UserState ) );
end;
procedure TCustomBackgroundWorker.RunWorkerAsync;
begin
RunWorkerAsync( TValue.Empty );
end;
procedure TCustomBackgroundWorker.RunWorkerAsync( Argument: TValue );
begin
if IsBusy
then
raise EInvalidOpException.Create( SWorkerIsBusy );
FCancellationPending := False;
FDoWorkEventArg := TDoWorkEventArgs.Create( Argument );
FThread := TThread.CreateAnonymousThread(
procedure
begin
NotifyDoWork( FDoWorkEventArg );
end );
FThread.OnTerminate := WorkerThreadTerminate;
FThread.Start;
end;
procedure TCustomBackgroundWorker.RunWorkerAsync<T>( Argument: T );
begin
RunWorkerAsync( TValue.From<T>( Argument ) );
end;
procedure TCustomBackgroundWorker.WorkerThreadTerminate( Sender: TObject );
var
LThread : TThread;
LDoWorkEventArg: TDoWorkEventArgs;
begin
LThread := FThread;
LDoWorkEventArg := FDoWorkEventArg;
FThread := nil;
FDoWorkEventArg := nil;
try
if Assigned( LThread.FatalException )
then
NotifyRunCompleted( TRunWorkerCompletedEventArgs.Create( TValue.Empty, LThread.FatalException as Exception, False ) )
else
NotifyRunCompleted( TRunWorkerCompletedEventArgs.Create( LDoWorkEventArg.Result, nil, LDoWorkEventArg.Cancel ) );
finally
FreeAndNil( LDoWorkEventArg );
end;
end;
{$ENDREGION}
end.