Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - Creating a Windows Service in Delphi
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Creating a Windows Service in Delphi
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 57
    2006-02-08

    Table of Contents:
  • Creating a Windows Service in Delphi
  • Let’s Create a Service
  • The Code
  • Installing/Uninstalling the Service
  • Entire Code for the Service Application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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!

    More Delphi-Kylix Articles
    More By Jacques Noah


       · Good article! This would help me get started in writing Services together with Indy...
       · I've provided the entire program in a zip archive for donwload. Enjoy!
       · i' very grateful to you, if u send me sample windows service application in...
       · Good Enough for me to start with servicesThanks.A little bit of more complicated...
     

    DELPHI-KYLIX ARTICLES

    - 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...
    - Using the Client Dataset in Two-Tiered Clien...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT