With this step we shall add a timer to our service module.
This timer will beep every second when the service runs. Go to the service
module source and do the following changes with it.
At first, a very important note: do not place
a TTimer component onto the service module. 1 It won`t work !
The thing is that the TTimer component lives in the same
thread in which it was created. The service module is created by the
Application.CreateForm call and this call is executed from the main
thread of the application. The service lives in a separate thread and
it is not a main thread of application. So the TTimer won`t work in
the service thread in spite of it`s placed on service module. In addition
the main thread of the service application stops and waits until all
services of application are terminated. Due to this reason there will
be no OnTimer events at all !
Now you are ready to make necessary changes. At first,
add the ExtCtrls unit to the "uses" section of the service module unit.
The necessary changes are marked by backgrounf color below.
|
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Dialogs,
Controls,
SvCom_NTService,
ExtCtrls;
|
Now add the Timer variable to the service class. Add the
declaration of the Timer event handler too. Necessary changes looks
like this:
|
type
TSampleService1 = class(TNTService)
private
{ Private declarations }
Timer: TTimer;
procedure TimerOnTimer(Sender: TObject);
public
{ Public declarations }
end;
|
Now add the implementation of TimerOnTimer method. In
this first example it will beep and nothing more. The necessary changes
of the "implementation" section should look as follows
|
implementation
{$R *.DFM}
procedure TSampleService1.TimerOnTimer(Sender: TObject);
begin
Beep;
end;
end.
|
Finally add the service OnStart and OnStop handlers. In
the OnStart event handler the Timer should be created and its OnTimer
property should be set to the TimerOnTimer. In the OnStop event the
Timer variable should be free. Because these events occur in the service
thread the timer created in such a way will work in the service thread.
It will be real " timer-in-service". Use Object Inspector to add the
corresponding event handlers to the service module. Add the following
code to them:
.
.
.
.
.
.
.
.
.
.
.
. |
procedure TSampleService1.TimerOnTimer(Sender: TObject);
begin
Beep;
end;
procedure TSampleService1.SampleService1Start(Sender: TNtService;
var DoAction: Boolean);
begin
Timer:=TTimer.Create(nil);
Timer.OnTimer:=TimerOnTimer;
end;
procedure TSampleService1.SampleService1Stop(Sender: TNtService;
var DoAction: Boolean);
begin
Timer.Free;
Timer:=nil;
end;
end. |
Now your service is ready. Save project and compile it.
With the next steps we`ll install and test it.
Ref 1: This statement was valid till
SvCom version 5.0, currently it should be changed as follows: the TTimer
on service module will not work at all if the Application.Threaded property
is set to False. In other case it will generate OnTImer events in the
main application thread, not in the service thread. It can cause severe
thread-safaty problems if OnTimer handler deals with the same objects
as the service does.