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
 servicethread.ProcessRequests(true);

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!

JustinWillis.
LVL 1
JustinWillisAsked:
Who is Participating?
 
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 (bas_gooijen@yahoo.com)

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

Copyright:
  Freeware for all use

History:
  Feb 16, 2002: Created.

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

uses
  SvcMgr,
  forms,
  windows,
  sysutils,
  ServiceUnit in 'ServiceUnit.pas' {SampleSvc: TService},
  FormUnit in 'FormUnit.pas' {Form2},
  DataModuleUnit in 'DataModuleUnit.pas' {DataModule1: TDataModule};

{$R *.RES}

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

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' ) ;

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

begin
  if NeedToRunAsService then
  begin  // we need the service
    SvcMgr.Application.Initialize;
    SvcMgr.Application.CreateForm(TSampleSvc, SampleSvc);
    SvcMgr.Application.Run;
  end
  else
  begin // the form needs to be created
    forms.Application.Initialize;
    forms.Application.CreateForm( TForm2, Form2 ) ;
    forms.Application.Run;
  end;
end.
0
 
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.

JustinWillis.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.