An advanced service application | ||||||||
Step 2. Adding an advanced event handlers | ||||||||
Go to the Object Inspector, select the Events tab and add empty event handlers to the OnStart, OnStop, OnPause, OnContinue and OnExecute events. Below we`ll add a code to these 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 them as simple as possible. Namely if changing of service state 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 this 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.
Do analogous changes with OnStop, OnPause and OnContinue event handlers. OnStop handler changes:
OnPause handler changes:
OnContinue handler changes:
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 | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | >> |
© 1998-2014
Alexey Dynnikov |