Converting a Delphi application to run as a service - is it necessary?

后端 未结 6 1739
醉话见心
醉话见心 2020-12-23 18:10

I have a delphi app that logs data from various places and writes the data to a file. The app has quite an extensive GUI to allow display of the data, configuration of the

6条回答
  •  悲&欢浪女
    2020-12-23 19:01

    I usually create my application in such a way that it can be started as a service or as a GUI, via a commandline switch /GUI.

    When the application runs with a GUI, I instantiate and start the service class "manually".

    Advantages:

    • It'll run the same code, making it very easy to debug the service. You can just put breakpoints and step through your code without having to "attach" to a running application.

    • Because of the GUI, you can see what your service would be doing, and interact with it, via listviews and buttons, even on remote servers where you don't have a debugger. Having to interact with your service via logs and configurations is crappy and slow.

    Example dpr, from a project that works like this:

    program xxxx;
    
    uses 
      SysUtils,
      SvcMgr,
      .......;
    
    {$R *.res}
    
    begin
      GlobalAppId := 1;
      MapMatcherController := TMapMatcherController.Create(nil);
      try
        if FindCmdLineSwitch('GUI',['/','-'],True) then
        begin
          Forms.Application.Initialize;
          Forms.Application.MainFormOnTaskbar := True;
          Forms.Application.CreateForm(TfrmMain, frmMain);
          Forms.Application.Run;
        end
        else
        begin
          SvcMgr.Application.Initialize;
          SvcMgr.Application.CreateForm(TsrvMapMatcher2, srvMapMatcher2);
          SvcMgr.Application.Run;
        end;
      finally
        MapMatcherController.Free;
     end;
    end.
    

    Oh, another thing to keep in mind is that services usually run as a "system" user, which means you'll have different privileges and settings (for example drive letter mappings).

提交回复
热议问题