Run a Service?

Hi guys, I have an application which I wanted to have the ability to run as a service, I have accomplished this by creating a new Service Application and then adding my units/forms to this app, all works great as a service now, BUT and there is always a but isn't there?? now I would like it to also run as a normal app when double clicked, is it possible to have it both ways? I would hate to have to maintain 2 seperate copies of it, want to be able to run it normally as a windows app, but if people want to use it often and when pc is not logged on then I want them to have the ability also to run /install to install it as service.

I realise I will have to add some intelligence to it so it knows if it is running as a service or not when closing (disable the forms close button as service should be stopped instead etc), current problem is if I run the application (not by starting service) it opens and then closes straight away, I think the service part is causing this as I guess it thinks it has nothing to do :/

I currently have the following in the Services OnExecute event..

 while not Terminated do

which seems to keep it alive until stopped when run as a service, why wont this do the same when just run as normal?

Please let me know if you want me to clarify anything, I am sure I am doing something pretty stupid here, not that familiar with Services im afraid.

Thanks in advance!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheRealLokiSenior DeveloperCommented:
Basically you need to edit the dpr (project-view source) and add some checking logic to see if it is being run as a service

here's some source from a demo. let me know if you require the whole thing

Sample of how to integrate a service and a normal applications in one executable.
When started (either as service or as normal app), the datamodule is created and the
program will beep every 5 seconds.

Created by: Bas Gooijen (

  Use it at your own risk, it could contain bugs.

  Freeware for all use

  Feb 16, 2002: Created.

If you found any bugs, have comments, or need help, just mail me.
program SampleService;

  ServiceUnit in 'ServiceUnit.pas' {SampleSvc: TService},
  FormUnit in 'FormUnit.pas' {Form2},
  DataModuleUnit in 'DataModuleUnit.pas' {DataModule1: TDataModule};

{$R *.RES}

function GetUserName: string;
  buff: array[byte] of char;
  sizeNeeded: dword;
  sizeNeeded := sizeof( buff ) - 1;
  windows.GetUserName( buff, sizeNeeded ) ;
  result := copy( buff, 1, sizeNeeded ) ;

function NeedToRunAsService: boolean;
var a:integer;cmd:string;
begin // this could be implemented in several ways, eg. check for a parameter with ParamStr().
  result := ( uppercase( GetUserName ) = 'SYSTEM' ) ;

  if (ansipos('INSTALL',cmd)<>0) or
     (ansipos('UNINSTALL',cmd)<>0) then
       result:=true;   // we are installing or uninstalling the service

  if NeedToRunAsService then
  begin  // we need the service
    SvcMgr.Application.CreateForm(TSampleSvc, SampleSvc);
  begin // the form needs to be created
    forms.Application.CreateForm( TForm2, Form2 ) ;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JustinWillisAuthor Commented:
Brilliant, works great, of course to stop the service closing the app just don't create it! love it.

Thanks for your help.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.