Products:

- SvCom
- Shell+
- PerfUtils

Download:

- SvCom
- Shell+
- PerfUtils

Ordering:

- Order now

Support:

- Contact directly


Embarcadero Technology Partner

 
    Step 3. Setting up service events  

Go to the Object Inspector, select the Events tab and add empty event handlers for OnStart, OnStop, OnPause, OnContinue and OnExecute events. Below you'll add a code to these event handlers.

Setting service event handlers

As specified by Win32 SDK if the service changes state and takes a lot of time such service should report that it is alive periodically. The rules of this reporting are not too complicated but the SvCom makes this operation simple as possible. Namely if service state change takes some time the service should periodically call the ReportStatus method. The time interval between subsequent ReportStatus calls should not exceed the WaitHint value.

To illustrate it let's add a time-consuming code to the service OnStart event handler. In our case it will be sleeping (it takes a lot of time, isn't it?). Necessary changes in the source are shown below. They are marked by background color.





 
 

 

 

 

procedure TSampleService2.SampleService2Start(Sender: TNtService;
  var DoAction: Boolean);
var i: Integer;
begin
    for i:=1 to 10 do
    begin
        Sleep(1000);  // It is our "very important" work
                      // that should be done before service stops 
        ReportStatus; // Hey, we are alive !
    end;
end;

Do similar changes with OnStop, OnPause and OnContinue event handlers.

OnStop handler changes:





 



 

 

 

 

procedure TSampleService2.SampleService2Stop(Sender: TNtService;
  var DoAction: Boolean);
var i: Integer;
begin
    // The service behavior at stop is under the same
    // rules as its starting behavior.
    // For example we can do the same actions as OnStart.
    for i:=1 to 10 do
    begin
        Sleep(1000);  // It is our "very important" work
                      // that should be done before service stops 
        ReportStatus; // Hey, we are alive !
    end;
end;

OnPause handler changes:





 
 

 

 

 

procedure TSampleService2.SampleService2Pause(Sender: TNtService;
  var DoAction: Boolean);
var i: Integer;
begin
    for i:=1 to 10 do
    begin
        Sleep(1000);  // It is our "very important" work
                      // that should be done before service stops 
        ReportStatus; // Hey, we are alive !
    end;
end;

OnContinue handler changes:





 
 

 

 

 

procedure TSampleService2.SampleService2Continue(Sender: TNtService;
  var DoAction: Boolean);
var i: Integer;
begin
    for i:=1 to 10 do
    begin
        Sleep(1000);  // It is our "very important" work
                      // that should be done before service stops 
        ReportStatus; // Hey, we are alive !
    end;
end;

Save changes and test this example. See the "Simple NT Service example" steps 5 and 6 to learn how to install and run or debug SvCom application. You will see that the service starting, stopping, pausing or continuing takes about ten seconds but no errors occur. Try to change the waiting count from 10 to 20, recompile your example and try it again. It works. Now try to comment the ReportStatus call, recompile project and try to start it. You will see that Windows reports that the error occurs during the service startup.

The conclusion is simple: if you need to perform time-consuming operations while the service changes it`s state do not forget to call the ReportStatus periodically. This approach can be used in OnStart, OnStop, OnPause and OnContinue event handlers. The next step shows how to use the OnExecute event handler correctly to do something when the service runs.

 

<< . index . 1 . 2 . 3 . 4 . 5 . 6 . >>
SvCom links:

- Overview
- Download
- Purchase
- FAQ
- Tutorial
- What's new
- For registered users
- Support

Copyright:

© 1998-2003. Alexey Dynnikov
© 2003-2016. ALDYN Software
© Design: Adonis i-cafe.