?
Solved

Run a Service?

Posted on 2006-04-10
2
Medium Priority
?
259 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:JustinWillis
2 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1000 total points
ID: 16422119
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
 
LVL 1

Author Comment

by:JustinWillis
ID: 16424666
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question