开发者

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


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.


If you've created a service, you can install it by running it with parameter /install

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

You can then start it, and you can debug it by attaching the debugger to it.

However, it's painful to work like that. I can't believe that some people actually work like that. I usually have all my business logic in separate units that I can run from a "normal" application. Only when that works out good, I wrap it in a service and try that.

Sometimes I even create an application that can run both as a service or with a GUI. You can simply instantiate your service class. You just need to start it yourself, but it'll be a lot easier to debug.


There is commercial solution that was developed specifically to simplify service debugging process. You can debug your code directly from the Delphi IDE, including service's OnStart event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜