Home arrow Delphi-Kylix arrow Page 3 - Creating a Windows Service in Delphi
DELPHI-KYLIX

Creating a Windows Service in Delphi


Service applications let you check for updates on a system or monitor system resources, without interrupting your work. While they can be difficult to create, Delphi makes it simple. This article explains how to create a simple Windows service application in Delphi, which you can build on and modify to suit your own needs.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 93
February 08, 2006
TABLE OF CONTENTS:
  1. · Creating a Windows Service in Delphi
  2. · Let’s Create a Service
  3. · The Code
  4. · Installing/Uninstalling the Service
  5. · Entire Code for the Service Application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating a Windows Service in Delphi - The Code
(Page 3 of 5 )

Once you’ve set up the properties, switch to the events tab. Let's go through the event options:

Before InstallAfter Install and Uninstall allow you to create actions that will take place during these events.

Start, Stop, Pause, and Continue are all key events that let you interact with the Service from the Service Control Manager or Task Manager. You can start, stop or pause the service in the Service Control Manager.

OnExecute is an event that is called when the Service is started. This is where we will write our code skeleton.

Now that we’ve covered the properties and events that enable you to create a Service application, let’s start to build our service.

Double click onExecute and add the following code:


procedure TService1.ServiceExecute(Sender: TService);
begin
Timer1.Enabled := True;
while not Terminated do
ServiceThread.ProcessRequests(True);// wait for termination
Timer1.Enabled := False;
end;

All that this procedure does is start up a thread that enables or disables the timer. Next we are going to set the timer and add the code to carry out the simple task, so click on the timer, go to the events tab and double click on onTimer, and add the following code:


procedure TService1.Timer1Timer(Sender: TObject);
const
FileName = 'c:\logdate.txt';
var
F: TextFile;
begin
AssignFile(f,FileName);
if FileExists(FileName) then Append(f)
else
Rewrite(f);
writeln(f,DateTimeToStr(Now));
ShowMessage(DateTimeToStr(Now));
CloseFile(f);
end;

That’s it as far as the code is concerned. Remember to remove the ‘ShowMessage(DateTimeToStr(Now));’ line in the TService1.Timer1Timer procedure, to avoid getting a MessageBox every minute.  Now, all we need to do is first, compile the code (to compile – press Ctrl +F9), then install the service and give it a test run!


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials