Solved

Service Application Help

Posted on 2004-04-14
9
344 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now