//unit Untservice;InterfaceusesWindows, Messages, Sysutils, Classes, Graphics, Controls, Svcmgr, Dialogs, DB, Adodb,registry,activex;typeTService2=class(TService) con1:tadoconnection; procedureServiceexecute (Sender:tservice); procedureServiceafterinstall (Sender:tservice); procedureServicecontinue (Sender:tservice;varContinued:boolean); procedureServicestart (Sender:tservice;varStarted:boolean); procedureServicepause (Sender:tservice;varPaused:boolean); Private {Private Declarations} Public functionGetservicecontroller:tservicecontroller;Override; {Public Declarations} End;varService2:tservice2;Implementation{$R *. DFM}procedureServiceController (Ctrlcode:dword);stdcall;beginService2.controller (ctrlcode);End;functionTService2.GetServiceController:TServiceController;beginResult:=ServiceController;End;procedureTservice2.serviceexecute (sender:tservice);begin while notTerminated Do beginservicethread.processrequests (True); //Do some thing; //here VCL creates a thread and implements a message pump to receive messages in the thread //You can add the loop detection event here.Sleep ( -); End;End;procedureTservice2.serviceafterinstall (sender:tservice);varRegistry:tregistry; key:string;beginRegistry:= Tregistry.Create; TryRegistry.rootkey:=HKEY_LOCAL_MACHINE; Key:='system/currentcontrolset/services/'+Name; ifRegistry.keyexists (Key) Then beginRegistry.openkey (key, True); Registry.writestring ('Description','xxxxxxxxxxxxx'); Registry.closekey; End; finallyRegistry.free; End;End;procedureTservice2.servicecontinue (Sender:tservice; varContinued:boolean);begincontinued:=True;End;procedureTservice2.servicestart (Sender:tservice;varStarted:boolean);beginCoInitialize (Nil); TryCon1. Open; exceptLogMessage ('Database Connection Error'); End; Started:=True;End;procedureTservice2.servicepause (Sender:tservice;varPaused:boolean);beginPaused:=True;End;End.
Reference: http://www.cnblogs.com/key-ok/p/3358916.html
Write a template for a service program (many can be found online)