0
votes

Hi i have created windows services in Delphi. I have installed and started the Services. Every thing working fine. Even i can check it in Task Manager.My service is Running.

But the code which i included inside the OnExecute method is not working.

My whole Code:

unit MyService;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
  Vcl.ExtCtrls,Jira;

type
  TJiraTestlink = class(TService)
    procedure ServiceExecute(Sender: TService);

  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  JiraTestlink: TJiraTestlink;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  JiraTestlink.Controller(CtrlCode);
end;

function TJiraTestlink.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TJiraTestlink.ServiceExecute(Sender: TService);
const
  SecBetweenRuns = 10;
var
  Count: Integer;
begin
  Count := 0;
  while not Terminated do
  begin
    Inc(Count);
    if Count >= SecBetweenRuns then
    begin
      Count := 0;

      { place your service code here }
      { this is where the action happens }
      ShowMessage(DateTimeToStr(Now));

    end;
    Sleep(1000);
    ServiceThread.ProcessRequests(False);
  end;
end;
end.

I am not sure where i did mistake. Any help will be appreciated. Thanks.

1
It doesn't count? or it doesn't show the message? For the latter, use OutputDebugString with DebugView or similar or write to a file or search for service desktop interaction vista... - Sertac Akyuz
If you are running it on XP or W2000, you have to set the interactive flag to enable ShowMessage from your service. On Vista and above you can't have an interactive service like David said in his answer. - mg30rg
I am using Windows 7. - vinothkumar r

1 Answers

8
votes

Since Vista, services are isolated and run in session 0, a non-interactive session. Interactive processes run in different sessions, starting from session 1 for the first logged on user.

This means that you cannot use your service to show UI. Calls to ShowMessage cannot work, by design, in a service.

You will need to find some other way to debug your service. For instance logging messages to a text file. Or OutputDebugString and a viewer like SysInternals DebugView which is capable of catching these debug strings from different sessions.