Menu

[r660]: / trunk / Src / UWaitForActionUI.pas  Maximize  Restore  History

Download this file

429 lines (388 with data), 13.5 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
{
* UWaitForActionUI.pas
*
* Implements a class that executes an action in a thread and displays a dialog
* box if action takes more than a specified time to complete.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UWaitForActionUI.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UWaitForActionUI;
{$INCLUDE CompilerDefines.inc}
interface
uses
// Delphi
Classes, ExtCtrls, Forms,
// Project
UBaseObjects;
type
{
TWaitForActionUI:
Class that executes an action in a thread and displays a dialog box if
action takes more than a specified time to complete. Any exception raised
by action is re-raised from main thread.
}
TWaitForActionUI = class(TNoPublicConstructObject)
strict private
fMinDisplayTimer: TTimer;
{Timer control used to delay closure of form if necessary}
fSaveFormShow: TNotifyEvent;
{Records any existing OnShow handler assigned to form referenced in Form
property}
fFormCloseRequested: Boolean;
{Flag indicating whether form is awaiting closure}
fAction: TBasicAction;
{Value of Action property}
fForm: TForm;
{Value of Form property}
fPauseBeforeDisplay: Cardinal;
{Value of PauseBeforeDisplay property}
fActionTerminated: Boolean;
{Flag indicating if action thread has terminated}
fMinTimeElapsed: Boolean;
{Flag indicating if minimum form display time has elapsed}
procedure ActionTerminated(Sender: TObject);
{Handles action thread's OnTerminate event. Flags thread has terminated
and requests closure of any displayed form.
@param Sender [in] Not used.
}
procedure MinTimeElapsed(Sender: TObject);
{Timer event handler called once, when minimum execution time has elapsed.
@param Sender [in] Not used.
}
procedure ShowOrHideForm(const Show: Boolean);
{Attempts to show or hide the form associated with the Form property. This
method should be called instead of ShowForm or CloseForm. The method is
runs inside a critical section.
@param Show [in] Flag that indicates whether form is to be shown (True)
or hidden (False).
}
procedure ShowForm;
{Displays any form assigned to Form property. Should not be called
directly: always call ShowOrHideForm(True) instead.
}
procedure CloseForm;
{Closes any form referenced by Form property, if action thread has
terminated and minimum display time has elapsed. Should not be called
directly: always call ShowOrHideForm(False) instead.
}
procedure FormShowHandler(Sender: TObject);
{Event handler for Form property's OnShow event. Checks if form can open.
@param Sender [in] Reference to form triggering event.
}
procedure Pause;
{Delays execution by PauseBeforeDisplay ms.
}
strict protected
constructor InternalCreate(const AAction: TBasicAction; const AForm: TForm;
const APauseBeforeDisplay, AMinDisplayTime: Cardinal);
{Class constructor. Sets up object.
@param AAction [in] Action to be executed.
@param AForm [in] Form to be displayed while action executes.
@param APauseBeforeDisplay [in] Time action given to execute before form
is displayed (optional)
@param AMinDisplayTime [in] Minimum time to display form (optional).
}
procedure Execute;
{Executes action and displays dialog box if necessary.
}
public
destructor Destroy; override;
{Class destructor. Tears down object.
}
class procedure Run(const AAction: TBasicAction; const AForm: TForm;
const APauseBeforeDisplay: Cardinal = 50;
const AMinDisplayTime: Cardinal = 1000);
{Creates and executes a TWaitForActionUI object that executes an action,
displaying dialog box if necessary.
@param AAction [in] Action to be executed.
@param AForm [in] Form to be displayed while action executes.
@param APauseBeforeDisplay [in] Time action given to execute before form
is displayed (optional)
@param AMinDisplayTime [in] Minimum time to display form (optional).
}
end;
implementation
uses
// Delphi
SysUtils, SyncObjs,
// Project
UThreadEx, UUtils;
type
{
TActionThread:
Class that executes an action in a thread.
}
TActionThread = class(TThreadEx)
strict private
fAction: TBasicAction;
{Action to be executed in thread}
fExceptObj: Exception;
{Value of ExceptObj property}
procedure DoAction;
{Checks action is assigned and executes it if so.
}
strict protected
procedure Execute; override;
{Causes action to be executed then sets completed flag when action
completes.
}
public
constructor Create(const AAction: TBasicAction);
{Class constructor. Sets up thread.
@param AAction [in] Action to be executed in thread.
}
property ExceptObj: Exception read fExceptObj;
{Reference to any exception that occured when action was executed. If
there was no exception the property is nil}
end;
var
// Critical section that ensures form opening / closing in TWaitForActionUI
// can only be run by one thread at a time
Lock: TCriticalSection;
{ TWaitForActionUI }
procedure TWaitForActionUI.ActionTerminated(Sender: TObject);
{Handles action thread's OnTerminate event. Flags thread has terminated and
requests closure of any displayed form.
@param Sender [in] Not used.
}
begin
fActionTerminated := True;
ShowOrHideForm(False); // close form (thread safe call)
end;
procedure TWaitForActionUI.CloseForm;
{Closes any form referenced by Form property, if action thread has terminated
and minimum display time has elapsed. Should not be called directly: always
call ShowOrHideForm(False) instead.
}
begin
Assert(Assigned(fForm), // ** do not localise
ClassName + '.CloseForm: fForm is nil');
Lock.Acquire;
try
fFormCloseRequested := fActionTerminated and fMinTimeElapsed;
if fFormCloseRequested and fForm.Visible then
fForm.Close;
finally
Lock.Release;
end;
end;
destructor TWaitForActionUI.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fMinDisplayTimer);
inherited;
end;
procedure TWaitForActionUI.Execute;
{Executes action and displays dialog box if necessary.
}
var
Thread: TActionThread; // thread used to execute action
begin
// Flag that form closure not yet requested
fFormCloseRequested := False;
// Reset termination flags
fActionTerminated := False;
fMinTimeElapsed := False;
// Execute action in thread
Thread := TActionThread.Create(fAction);
try
Thread.OnTerminate := ActionTerminated;
{$IFDEF ThreadResumeDeprecated}
Thread.Start;
{$ELSE}
Thread.Resume;
{$ENDIF}
// Pause before displayng dialog by PauseBeforeDisplay ms
Pause;
if not Thread.Completed then
// Show dialog if thread not completed.
ShowOrHideForm(True); // shows form, protected by critical section
// Re-raise any exception that was raised by action thread
if Assigned(Thread.ExceptObj) then
raise Thread.ExceptObj;
finally
FreeAndNil(Thread);
end;
end;
procedure TWaitForActionUI.FormShowHandler(Sender: TObject);
{Event handler for Form property's OnShow event. Checks if form can open.
@param Sender [in] Reference to form triggering event.
}
begin
// Call any earler OnShow handler for form
if Assigned(fSaveFormShow) then
fSaveFormShow(Sender);
// If form has already been requested to close then close it
if fFormCloseRequested then
fForm.Close;
end;
constructor TWaitForActionUI.InternalCreate(const AAction: TBasicAction;
const AForm: TForm; const APauseBeforeDisplay, AMinDisplayTime: Cardinal);
{Class constructor. Sets up object.
@param AAction [in] Action to be executed.
@param AForm [in] Form to be displayed while action executes.
@param APauseBeforeDisplay [in] Time action given to execute before form is
displayed (optional)
@param AMinDisplayTime [in] Minimum time to display form (optional).
}
begin
inherited InternalCreate;
// Set up timer that triggers event when form can close
fMinDisplayTimer := TTimer.Create(nil);
fMinDisplayTimer.Enabled := False;
fMinDisplayTimer.OnTimer := MinTimeElapsed;
fMinDisplayTimer.Interval := AMinDisplayTime;
// Record other fields
fPauseBeforeDisplay := APauseBeforeDisplay;
fAction := AAction;
fForm := AForm;
end;
procedure TWaitForActionUI.MinTimeElapsed(Sender: TObject);
{Timer event handler called once, when minimum execution time has elapsed.
@param Sender [in] Not used.
}
begin
// Switch off timer
fMinDisplayTimer.OnTimer := nil;
fMinDisplayTimer.Enabled := False;
fMinTimeElapsed := True;
ShowOrHideForm(False); // close form (thread safe call)
end;
procedure TWaitForActionUI.Pause;
{Delays execution by PauseBeforeDisplay ms.
}
begin
if fPauseBeforeDisplay = 0 then
Exit;
// Busy wait
UUtils.Pause(fPauseBeforeDisplay);
end;
class procedure TWaitForActionUI.Run(const AAction: TBasicAction;
const AForm: TForm; const APauseBeforeDisplay, AMinDisplayTime: Cardinal);
{Creates and executes a TWaitForActionUI object that executes an action,
displaying dialog box if necessary.
@param AAction [in] Action to be executed.
@param AForm [in] Form to be displayed while action executes.
@param APauseBeforeDisplay [in] Time action given to execute before form is
displayed (optional)
@param AMinDisplayTime [in] Minimum time to display form (optional).
}
begin
Assert(Assigned(AAction), // ** do not localise
ClassName + '.Run: AAction is nil');
with InternalCreate(AAction, AForm, APauseBeforeDisplay, AMinDisplayTime) do
try
Execute;
finally
Free;
end;
end;
procedure TWaitForActionUI.ShowForm;
{Displays any form assigned to Form property. Should not be called directly:
always call ShowOrHideForm(True) instead.
}
begin
Assert(Assigned(fForm), // ** do not localise
ClassName + '.ShowForm: fForm is nil');
// Start timer that triggers when form is allowed to close after minimum
// display time. If time is zero we need to trigger ourselves, because
// timer won't fire.
fMinDisplayTimer.Enabled := True;
if fMinDisplayTimer.Interval = 0 then
MinTimeElapsed(fMinDisplayTimer);
// Store any existing OnShow event of form
fSaveFormShow := fForm.OnShow;
fForm.OnShow := FormShowHandler;
try
// Show form modally if not already requested to close
if not (fFormCloseRequested) then
fForm.ShowModal;
finally
// Restore any previous OnShow event handler
fForm.OnShow := fSaveFormShow;
end;
end;
procedure TWaitForActionUI.ShowOrHideForm(const Show: Boolean);
{Attempts to show or hide the form associated with the Form property. This
method should be called instead of ShowForm or CloseForm. The method is runs
inside a critical section.
@param Show [in] Flag that indicates whether form is to be shown (True) or
hidden (False).
}
begin
Lock.Acquire;
try
if Assigned(fForm) then
begin
if Show then
ShowForm
else
CloseForm;
end;
finally
Lock.Release;
end;
end;
{ TActionThread }
constructor TActionThread.Create(const AAction: TBasicAction);
{Class constructor. Sets up thread.
@param AAction [in] Action to be executed in thread.
}
begin
inherited Create(True);
fAction := AAction;
end;
procedure TActionThread.DoAction;
{Checks action is assigned and executes it if so.
}
begin
if Assigned(fAction) then
fAction.Execute;
end;
procedure TActionThread.Execute;
{Causes action to be executed and records any exception that was raised.
}
begin
fExceptObj := nil;
try
// Action may not be thread safe so we execute it in calling thread
Synchronize(DoAction);
except
// Keep reference to any exception
fExceptObj := AcquireExceptionObject;
end;
end;
initialization
// create critical section
Lock := TCriticalSection.Create;
finalization
// disposes of critical section
Lock.Free;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.