0

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?

Bjarke Moholt
  • 313
  • 1
  • 4
  • 20
  • 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 Sep 30 '14 at 08:44
  • @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 Sep 30 '14 at 10:41
  • a loop with `WaitForSingleObject`/`WaitForMultipleObjects` would be better... – whosrdaddy Sep 30 '14 at 10:45
  • I've run an empty service now, still Error 1053 – Bjarke Moholt Sep 30 '14 at 10:59
  • 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 Sep 30 '14 at 11:02
  • Also, you might consider looking over your previous questions and seeing if there are answers that you could accept – David Heffernan Sep 30 '14 at 11:08

2 Answers2

4

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).

Community
  • 1
  • 1
whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
0

(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

Bjarke Moholt
  • 313
  • 1
  • 4
  • 20
  • Why are you using runtime packages in a service application? – whosrdaddy Oct 14 '14 at 11:38
  • Because the packages contained functionality I wanted to reuse. It was my first service and I have had a good deal of trial and error. Now I can see why it may not be the best idea going forward, so I've removed the packages and trimmed the service down – Bjarke Moholt Oct 14 '14 at 11:55
  • Anyway, my answer is still correct, so keep that in mind. And as you can see, details matter when asking a question... – whosrdaddy Oct 14 '14 at 12:11