To the main page...The list of my products...Some texts...Sample applications, tips, tricks...If you need support...


 
An advanced service application
Step 5. How to show messages to user from a service

Usually services run in background and does not show anything to the user. But sometimes it is necessary to show some information to user or ask him about something. There are several ways to implement such behaviour. The message boxes are one of them. This example shows how to use message boxes from service. There are many cases in which using of message box is quite enough. In our example we shall ask user whether to stop service or not. If user confirms that service should be stopped it will stop. In other case the stopping request will be rejected and service will continue to run.

Modify the OnStop event handler as shown below.





 .
 .








 .
 .

 .

 .

 .

procedure TSampleService2.SampleService2Stop(Sender: TNtService;
  var DoAction: Boolean);
var i: Integer;
begin
    DoAction:=
        MessageBoxEx(0,
          'The SvCom example service will be stopped.'#13#10+
          'Are you sure ?','Service confirmation',
          MB_YESNO 
          or MB_ICONQUESTION 
          or MB_SERVICE_NOTIFICATION,
          0) = IDYES;

    if DoAction then
    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;

Note that we use the MessageBoxEx function instead of MessageBox or Delphi's MessageDlg. We use it because this function allows to show a message box from a service. In addition the MB_SERVICE_NOTIFICATION flag is necessary. It is this flag that says Windows that it should show message window in the user's desktop not in the desktop of service.

Another feature is usage of the DoAction parameter. By default it is True when the event handler is called. By setting it to False we prevent the service state from change.

<< | Index | Step 1 | Step 2 | Step 3 | Step 4 | Step 5


 
© 1998-2014 Alexey Dynnikov