1

working with a Windows Service application in Delphi, I stumbled on the issue as in the subject.

I do start a default Delphi Windows Service project on the IDE, follow the wizard and in the end I have a project and a TService unit. I add to this project another unit, a Data Module (named DM) in which the service code logic is contained. The DM has a TTimer (design-time) that runs a relatively long job.

Case 1: DM is created by default in design-time. I have the following code in my TService Start/Stop

procedure TOmegaCAOraNT.ServiceStart(Sender: TService; 
  var Started: Boolean); 
begin 
  DM.Timer1.Enabled := True; 
  Started := true; 
end; 

procedure TOmegaCAOraNT.ServiceStop(Sender: TService; var Stopped: Boolean);
begin 
  DM.Agent_Stop; 
  Stopped := true; 
end; 

When I try to Stop the Service via Windows SCM - in appearance it confirms the Stop, field Status becomes empty - but it does not. I can see the service .exe still running for a while, and what's more it does terminate the Timer's long job in the middle, doing part of it only! This is an undesired behavior!

I fixed this in the second case

Case 2: DM is created in run-time. The Timer is enabled on DM.OnCreate I have the following code in my TService Start/Stop

procedure TOmegaCAOraNT.ServiceStart(Sender: TService; var Started: Boolean); 
begin 
  FDataModule := TDM.Create(nil); 
  Started := true; 
end; 

procedure TOmegaCAOraNT.ServiceStop(Sender: TService; var Stopped: Boolean);
begin 
  FreeAndNil(FDataModule); 
  Stopped := true; 
end; 

When I try to Stop the Service via Windows SCM - it throws the following Warning:

"Windows could not stop the SERVICE> service on the Local Computer. The service did not return an error. his could be an internal Windows error or an internal service error. if the error persists, contact your system Administrator"

field Status remains Started. Timer' long job finishes until the end, and then the service really stops (refresh in SCM to see, Status empty) This is desired behavior !

My problem is that I would like the DM to be created in design-time, and not in run-time

My question is: can I have the right behavior of run-time DM (Case 2), with a design-time DM (Case 1) ?

thanks and best regards, Altin

4
  • What do you think is the difference? Why on earth should it be "Designtime"? You should find a way to tell your DM to stop if it is required. I'm not sure that you understand Services (and Threads) at all. Pick a paper and try to draft the flow.
    – Fritzw
    Dec 7, 2017 at 20:11
  • It should be design-time because the same DM.pas I want to use in another project - non-service, a normal application. With my knowledge in Delphi it is easier that way. I don't need my DM to stop at all - I need it to run until job done. regards Altin PS. Yes - I don't know of Services, that is why I am looking here for advise
    – altink
    Dec 7, 2017 at 20:17
  • start a thread on the onstart event, stop the thread in the onstop event and wait for the thread to finish... Its not really different from a normal program except for the fact that you don't interact with the GUI environment.
    – whosrdaddy
    Dec 7, 2017 at 20:20
  • thank you for the prompt response. is it the FreeAndNil(FDataModule) that makes the difference ? in the meaning it waits for things inside freed object to complete? will try with the Thread - but every programming book warns about their difficulty :-) regards Altin
    – altink
    Dec 7, 2017 at 20:31

1 Answer 1

6

TService runs in is own worker thread at run-time.

If you configure the DM to be auto-created, it (and its TTimer) will be created in the main thread at run-time, not in the service thread. And thus, the TTimer will run in the main thread, and can be activated only by the main thread, not in the TService.OnStart event handler (an EOutOfResources exception will be raised if you try).

If you manually create the DM in the TService.OnStart event handler, it (and its TTimer) will be created in the service thread, not in the main thread. The TTimer will run in the service thread, and can be (de)activated in the TService events.

Either way, make sure your TTimer.OnTimer event handler uses thread-safe code.

Also, the TService.OnStop event handler must call the TService.ReportStatus() method periodically (before the TService.WaitHint interval elapses) while waiting for other threads to stop whatever they are doing. Which means you shouldn't use thread-blocking code in the TService.OnStop event handler.

You are not handling this correctly, which is why the SCM is having problems.

1
  • thank you very much Mr. Lebeau I know you are always right, :-) , but in this area (services) I am sadly far from the right knowledge; I will go for the run-time DM as long as it satisfies my needs. thank you very much for your time, regards, Altin
    – altink
    Dec 7, 2017 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.