Creating a windows service in delphi

I created windows service in delphi. My code runs in ServiceExecute

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

Sorry, I cannot run this code. I don't seem to call this procedure even when I am debugging. The code in Myservice.exe is as follows.

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

I can run serviceExecute if I add

 MyService.ServiceExecute(nil); 

in MyService.exe, however, if I install it as a service, it does not seem to work as Application.Run does anything

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

thanks

+6
source share
3 answers

You cannot just start a service from the IDE to debug it; in this case, he just quits. The service must be started by the service control manager. In addition, you should not directly access ServiceExecute.

Here's the documentation on how to debug services.

+8
source

If you created a service, you can install it by running it with the /install option

After that, the service should appear between your other services (go to start/run/ and enter services.msc ).

Then you can run it, and you can debug it by attaching a debugger to it.

However, it hurts to work like that. I can’t believe that some people really work like that. Usually I have all my business logic in separate units that I can run from a “normal” application. Only when it works out well will I wrap it in the service and try it.

Sometimes I even create an application that can work both as a service and through a graphical interface. You can simply instantiate a class of service. You just need to run it yourself, but it will be much easier to debug it.

+4
source

There is a commercial solution designed specifically to simplify the process of debugging services. You can debug your code directly from the Delphi environment, including the OnStart service event.

+1
source

Source: https://habr.com/ru/post/888549/


All Articles