Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Windows service in delphi

I have created a windows service in delphi. My code is run within ServiceExecute

procedure TMyService.ServiceExecute(Sender: TService);
 while not Terminated do
 begin
      CallSomeProcedure;  
      Sleep(1000);
      ServiceThread.ProcessRequests(false);
 end;
end;

Unfortunately, I can not get this code to run. I doesn't seem to call the procedure even when I'm debugging. The code in Myservice.exe looks like this.

begin 
    if not Application.DelayInitialize or Application.Installing then
       Application.Initialize;
    Application.CreateForm(TMyService, MyService);
    Application.Run;
end.

I can get the serviceExecute to run if I add

MyService.ServiceExecute(nil);

into MyService.exe however if I install it as a service it appears not to be running as Application.Run does nothing

Not sure what I'm doing wrong, but any help would be much appreciated.

Thanks

like image 414
user761275 Avatar asked May 19 '11 15:05

user761275


People also ask

How to create a Windows service in Delphi?

How to create Windows Service in Delphi? To create a service application in Delphi start Delphi IDE and select File->New->Other. Then select "Service Application". Then you can see application which includes a Datamodule descendent Tservice class.

What is service application?

Applications as a service refers to the delivery of computer software applications as a service via the Internet. This type of software is also referred to as SaaS (Software as a Service), software on demand and on-demand software.


1 Answers

You cannot just run the service from the IDE to debug it; in that case it will just exit. The service has to be started by the service control manager. Also, you shouldn't be calling ServiceExecute directly.

Here's documentation how to debug services.

like image 94
Ondrej Kelle Avatar answered Oct 26 '22 10:10

Ondrej Kelle