NaN
NaN

Reputation: 9104

Why My Delphi Service App is not starting?

I created a new Windows Service project using wizard, putted some code, compiled it, run it with /INSTALL and then I tried to start it using net start myservice but I've got an service name not found error; then I went to the Control Panel in Services and when I try to start clicking the 'Start' link the dialog windows that shows up freezes at 50% of the progress bar indefinitely.

This is my first try to make a service to update the main system I am developing, and for a test I put a Timer to tell the time every one minute. Can anyone notice what is wrong and why it is behaving like that?

The DPR file with:

{...}
begin
  if not Application.DelayInitialize or Application.Installing then
  begin
    Application.Initialize;
  end;
  Application.CreateForm(TZeusUpdateSevice, ZeusUpdateSevice);
  Application.Run;
end.

and the PAS file with:

{...}
procedure ServiceController(CtrlCode: DWord); stdcall; 
begin
  ZeusUpdateSevice.Controller(CtrlCode);
end;

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

procedure TZeusUpdateSevice.ServiceAfterInstall(Sender: TService);
var
  regEdit : TRegistry;
begin
  regEdit := TRegistry.Create(KEY_READ or KEY_WRITE);
  try
    regEdit.RootKey := HKEY_LOCAL_MACHINE;

    if regEdit.OpenKey('\SYSTEM\CurrentControlSet\Services\' + Name,False) then
    begin
      regEdit.WriteString('Description','Mantém atualizados os arquivos e as credenciais da Plataforma Zeus.');
      regEdit.CloseKey;
    end;

  finally
    FreeAndNil(regEdit);
  end;
end;

procedure TZeusUpdateSevice.ServiceStart(Sender: TService; var Started: Boolean);
begin
{ executa os processos solicitados pelo sistema }
  Timer1.Enabled := True;
  while not Terminated do ServiceThread.ProcessRequests(True);
  Timer1.Enabled := False;
end;

procedure TZeusUpdateSevice.Timer1Timer(Sender: TObject);
begin
  ShowMessage('Now, time is: ' + TimeToStr(Now));
end;

Upvotes: 2

Views: 5323

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

There are a couple of obvious problems:

  1. You have an infinite loop in the OnStart event. This event allows you to perform one time actions when the service starts. That code belongs in OnExecute.
  2. Services cannot show UI and so ShowMessage cannot work. You'll need to use a non-visual mechanism to give feedback.

Because your OnStart doesn't return, the SCM regards your service as not having started. So I guess that item 1 above is the explanation as to why your service won't start.

Upvotes: 12

Related Questions