Menu

[r3201]: / trunk / Src / Notifications.URecorderThread.pas  Maximize  Restore  History

Download this file

138 lines (117 with data), 3.9 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines an abstract base class for threads that generate and record
* notification data in the notification queue.
}
unit Notifications.URecorderThread;
interface
uses
// Delphi
Classes,
// Project
Notifications.UData;
type
/// <summary>Abstract base class for threads that generate or receive a
/// notification that is to be recorded in the notification queue.</summary>
/// <remarks>Sub-classes must override GetNotification to provide any
/// notification data record to be queued.</remarks>
TNotificationRecorderThread = class abstract(TThread)
strict private
var
/// <summary>Number of milliseconds to delay start of thread's execution.
/// </summary>
fStartDelay: Cardinal;
/// <summary>Performs a busy wait for the time specified by fStartDelay.
/// </summary>
procedure DoDelay;
strict protected
/// <summary>Gets notification data to be queued.</summary>
/// <param name="N">TNotificationData [out] Set to required notification
/// data item.</param>
/// <returns>Boolean. True on success or False if no notification data is
/// available.</returns>
/// <remarks>N can be undefined if False is returned.</remarks>
function GetNotification(out N: TNotificationData): Boolean; virtual;
abstract;
/// <summary>Stores the given notification data item in the notification
/// queue.</summary>
/// <remarks>If notification can't be queued it is discarded.</remarks>
procedure RecordNotification(const N: TNotificationData);
/// <summary>Gets and stores a notification in the notification queue.
/// </summary>
/// <remarks>Called when thread is started.</remarks>
procedure Execute; override;
public
/// <summary>Creates the and initialises the thread instance.</summary>
/// <param name="StartDelay">Cardinal [in] Number of milliseconds to delay
/// the start of the thread's execution. Passing 0 indicates no delay.
/// Non-zero delays are rounded up to the nearest 1/5th second.</param>
constructor Create(StartDelay: Cardinal = 0);
end;
implementation
uses
// Delphi
Windows,
// Project
Notifications.UQueue;
{ TNotificationRecorderThread }
constructor TNotificationRecorderThread.Create(StartDelay: Cardinal);
begin
inherited Create(True);
fStartDelay := StartDelay;
end;
procedure TNotificationRecorderThread.DoDelay;
var
StartTC: Cardinal;
CurrentTC: Int64;
begin
StartTC := GetTickCount;
repeat
if Terminated then
Exit;
CurrentTC := Windows.GetTickCount;
if CurrentTC < StartTC then
CurrentTC := CurrentTC + High(Cardinal);
Sleep(200); // 1/5 second
until CurrentTC - StartTC >= fStartDelay;
end;
procedure TNotificationRecorderThread.Execute;
var
N: TNotificationData;
begin
if fStartDelay > 0 then
DoDelay;
if Terminated then
Exit;
if not GetNotification(N) or Terminated then
Exit;
RecordNotification(N);
end;
procedure TNotificationRecorderThread.RecordNotification(
const N: TNotificationData);
var
Queue: TNotificationQueue;
begin
Queue := TNotificationQueue.Create;
try
if Terminated then
Exit;
// Attempt to push notification data onto queue. This may fail if queue is
// full, in which case we ignore the problem and ditch the notification.
// If in future some other action needs to be taken when the queue is
// full, just test the return value of the Push method - False => queue
// full.
Queue.Push(N); // call this may block waiting for critical section
finally
Queue.Free;
end;
end;
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.