0
votes

I have a service made in Delphi XE that will not start when prompted from the service manager in Windows 7, I get

Error 1053: The service did not respond to the start or control reqquest in a timely fashion

I have the service hooked up with an AfterInstall and an OnExecute event, here is my code for the events:

procedure TAarhusRunner.ServiceAfterInstall(Sender: TService);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
  try

    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\SYSTEM\CurrentControlSet\Services\' + Name, false) then
    begin
      Reg.WriteString('Description', 'Worker Service for Inversion Job Distribution');
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TAarhusRunner.ServiceExecute(Sender: TService);
begin
  try
    Self.Status := csRunning;

    //start the loop
    MainTimer.Interval := 5000; //MainTimer is declared in the .dfm
    MainTimer.Enabled := True;
    RecheckAndApplyTimer.Enabled := False;
    while not Terminated do
    begin
       ServiceThread.ProcessRequests(true);
       MainTimer.Enabled := False;
    end;

  except
    on e: Exception do begin
      MessageDlg(E.Message,mterror,[mbok],0);
      exit;
    end;
  end;
end;

Can anyone tell me what I am doing wrong?

2
It will be good for you to learn how to debug this sort of thing. Start with a brand new, empty, service project. Does that start, or does that also fail with error 1053. That's the very first thing you should attempt to do. - David Heffernan
@TOndrej, You're certain of this? How would you propose a service that is performing in a loop with a delay then? (Please don't say thread.sleep) - Bjarke Moholt
a loop with WaitForSingleObject/WaitForMultipleObjects would be better... - whosrdaddy
I've run an empty service now, still Error 1053 - Bjarke Moholt
So why are you wasting our time with all this code?! What you should be asking is, why does a blank service project fail to start. I suggest that you ask that very question. When you ask that question, the very first thing I will ask is what user account is running the service. - David Heffernan

2 Answers

4
votes

you use

ServiceThread.ProcessRequests(True);

in your service loop with WaitForMessage set to True. This will block your loop since it will wait indefinitely for a service message.

To solve your problem, simply change your line to:

ServiceThread.ProcessRequests(False);

Some general advice:

Do not implement the OnExecute handler of a service but spawn a thread in the OnStart eventhandler instead. Terminate this thread from the OnStop Eventhandler. More details can be found here.

Using a TTimer from a non GUI thread (like the service thread in your case) is tricky, it is not impossible however (David Heffernan has a topic on this subject here on SO).

0
votes

(Solved) It turned out to be a unit error that prevented the service from responding. I copied the relevant .bpl package to the service folder and that seemed to solve the error.

Thank you all for taking the time to add your input