Solved

Service Application Help

Posted on 2004-04-14
9
348 Views
Last Modified: 2010-04-05
Hey Everyone,

For the last few days I have been trying to port parts of a current application of mine to a service. There are a couple problems:

1) I dont know anything about services (I know what they should do, but dont know how to write em). Added to this, hours of googling turned up nothing relavant.

2)I have got the thing running, and all appearances seem well and good, but no code will execute on startup, and the service terminates itself after about 10 seconds.

Ive chucked the ol ServiceThread.ProcessRequests(False); in the ServiceStart section, but I cant find any good tuts that even explain why this should be done.

Added to this, I am using DBISAM, Windows XP, and Delphi 7. Anything DBISAM related would be really good.

Look forward to the much needed help...
0
Comment
Question by:odders
9 Comments
 

Accepted Solution

by:
G-Rega earned 125 total points
ID: 10830540
0
 
LVL 3

Author Comment

by:odders
ID: 10830713
Thank G-Rega, but not really what im after. It seemed a little outdated, and had a couple of components that it refernced to that I dont have. And I probably should have specified that im creating a non-interactive service.

Basically what im doing now is creating the service that will act as the database server for the rest of the clients, and admin programs. Extra components im using are Fpiettes Telnet control, DBISAM, and CoolTrayIcons. I know these seem pretty irrelevant, but if anyone knows any consideration that need to be taken into account becuase of these, or has some examples in context, it would be super helpful.
0
 
LVL 3

Expert Comment

by:MarcG
ID: 10831158
"2)I have got the thing running, and all appearances seem well and good, but no code will execute on startup, and the service terminates itself after about 10 seconds.

Ive chucked the ol ServiceThread.ProcessRequests(False); in the ServiceStart section, but I cant find any good tuts that even explain why this should be done."

Did you try someting like:

while True do
begin
  { some code of yours }
  ServiceThread.ProcessRequests(False);
end;

in the OnExecute Event?

When the OnExecute Event is finished the Service Thread is terminated. so to have a service run all the time you need a loop like this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:odders
ID: 10838225
put that in then, but executing still stops. this is what my onExecute looks like after the additions:

procedure TMaestroService.ServiceExecute(Sender: TService);
var
  iniRead: TIniFile;
  tnetHost, tnetPort: String;
begin
  areaCode := '02';
  readyState := False;
  MessageBoxEx(0, 'Connection Established', 'Confirmation', MB_YESNO or MB_ICONQUESTION or MB_SERVICE_NOTIFICATION, 0);

  iniRead := TIniFile.Create('maestrotel.conf');
  try
    tnetHost := iniRead.ReadString('ServiceSettings', 'IpAddress', '203.17.38.80');
    tnetPort := IntToStr(iniRead.ReadInteger('ServiceSettings', 'PortAddress', 23));
  finally
    iniRead.Free;
  end;

  tnetPrimary.Host := tnetHost;
  tnetPrimary.Port := tnetPort;
  tnetPrimary.Connect;
  InitiateData;

  while True do
    begin
      Sleep(1);
      ServiceThread.ProcessRequests(False);
    end;
end;

Oh, and I forgot one big detail, sorry; the message box ive got in the code doesnt appear, when in onExecute, or onStart. But if manually close the application (form the popup menu on the sys tray), the message box I have there is fired.
0
 
LVL 3

Author Comment

by:odders
ID: 10839454
and the app wont terminate when i call destroy. ive tried calling doDestroy, doShutdown, balh balh, but none work. heres the code behind the click:

procedure TMaestroService.itmExitClick(Sender: TObject);
begin
  if MessageBoxEx(0, 'Are you sure you want to close the MaestroTel Database Service?'#13#13'This may cause any connected client applications to terminate.', 'MaestroTel confirmation', MB_YESNO or MB_ICONQUESTION or MB_SERVICE_NOTIFICATION, 0) = IDYES then
    begin
      sysIcon.HideTaskbarIcon;
      MaestroService.Destroy;
    end;
end;

im upping the points to 250. i just need simple explaination of what importanat commands i need to know when using services, and what considerations i should be taking.
0
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 125 total points
ID: 10840896
in the OnExecute do a loop checking the Terminated property

while not Terminated do
begin
  // do stuff - if any
  Sleep(1);
  ServiceThread.ProcessRequests(false);
end;

and don't call Destroy ! not for any object .. use Free .. but for services use Terminate - it will set the Terminated property to true and the service thread loop will exit
also restrain yourself from using message boxes in the service
for logging purposes use the service's EventLog, for any user interaction create a separate GUI app which will talk to your service thru let's say TCP/IP (making the gui control internet ready)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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