[ACCEPTED]-Converting a Delphi application to run as a service - is it necessary?-stay-logged-in

Accepted answer
Score: 26

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

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

Advantages:

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

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

Example 5 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 4 thing to keep in mind is that services usually 3 run as a "system" user, which means you'll 2 have different privileges and settings (for 1 example drive letter mappings).

Score: 7

There are commercial (and free) solutions like 10 Firedaemon, which will run (almost) any application 9 as a service.

On a sidenote, it shouldn't 8 really be hard to separate logic and user 7 interface - you should've done that already 6 when you developed the application. Just 5 because Delphi makes it easy to write business 4 logic in the code behind associated with 3 the user interface, that doesn't mean you 2 should really do it. Have a look at the 1 presentation pattern's at Martin Fowler's site.

Score: 7

In general is possible to have a mixed single 23 exe which in turn runs as a service or runs 22 as a full GUI standard application.

How much 21 effort your application needs to fit this 20 category is matter of how is it designed, specially 19 in the kind of coupling it have between 18 business logic and user interface logic.

One 17 great example of this kind of application 16 comes with Delphi itself: scktsrvr.exe in your $DELPHI\bin 15 directory runs as a GUI application or as 14 a service (run scktsrvr.exe /install to 13 auto-register the service and use management 12 console to start/stop it.

in folder $DELPHI\source\db 11 you will find the project files (scktsrvr.dpr/res, ScktCnst.pas, ScktMain.pas/dfm). Take 10 your time to inspect how is it done and, who 9 knows... maybe this is what you're looking 8 for your application.

Take in account since 7 Windows Vista interactive services are not allowed to interact 6 with the user at his/her desktop. An administrator 5 have to enable the interactive services detection and the user have to 4 change to session 0 desktop in order to 3 interact with your service (by interact 2 it means to see and interact with your service 1 forms)

Score: 2

You can try to use svrany, a tools to run 2 an application as service. It's parts of 1 Server Resource Kit Tools. try this link for 2003 server resource kit download.

Score: 1

It depends a little on your application, but 5 generally it is achievable. Try this: http://iain.cx/src/nssm. Don't 4 forget to start all services that you application 3 depends on BEFORE you start your application 2 as a service. Google around for info on 1 how to do that.

Score: 1

You could write a simple service that starts 15 your application. But if you care about 14 your app future, I would go the service 13 way. Yes, you would have to split the application 12 in two pieces, the client/GUI part and the 11 service itself, especially since Vista and 10 7 made much more difficult for a service 9 to display a user interface for security 8 reasons. Services have several advantages, they 7 run in their separate session, they can 6 be set to run with a given user that could 5 be different from the logged-on one, only 4 user with proper privileges can control 3 them, Windows can automatically restart 2 them (or perfom other actions) when they 1 fail.

More Related questions