Solved

Service Application Help

Posted on 2004-04-14
9
342 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now