unit unit 2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
Vcl.ExtCtrls, System.DateUtils,System.StrUtils;
type
TService2 = class(TService)
Timer1: TTimer;
procedure ServiceExecute(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure Timer1Timer(Sender: TObject);
procedure ServiceCreate(Sender: TObject);
procedure ServiceDestroy(Sender: TObject);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
procedure WriteLog(cMsg:String);
{ Public declarations }
end;
var
Service2: TService2;
implementation
{$R *.dfm}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service2.Controller(CtrlCode);
end;
function TService2.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService2.ServiceCreate(Sender: TObject);
begin
Writelog('Create');
DoStart;
end;
procedure TService2.ServiceDestroy(Sender: TObject);
begin
Writelog('Destroy');
end;
procedure TService2.ServiceExecute(Sender: TService);
var
cnt : Integer;
begin
Try
Writelog('ServiceExecute');
cnt :=0;
Timer1.enabled := true;
while not self.Terminated do
begin
inc(CNT);
// WriteLog('Ejecutandose' + InttoStr(cnt));
ServiceThread.ProcessRequests(true);
// Sleep(10000);
end;
Timer1.Enabled := False;
except
on e:Exception do
begin
Writelog('ServiceExecute'+ e.Message);
Timer1.Enabled := False;
end;
end;
end;
procedure TService2.ServicePause(Sender: TService; var Paused: Boolean);
begin
Paused := true;
Writelog('Pausado');
end;
procedure TService2.ServiceStart(Sender: TService; var Started: Boolean);
begin
Started := true;
Writelog('Iniciado');
end;
procedure TService2.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
Stopped := true;
Writelog('Stopped');
end;
procedure TService2.Timer1Timer(Sender: TObject);
begin
Writelog('Timer');
end;
procedure TService2.WriteLog(cMsg:String);
var
myFile : TextFile;
cFile : string;
begin
Try
cFile := 'c:\temp\logTest.txt';
AssignFile(myFile, cFile);
if fileExists(cFile) then
Append(myfile)
else
ReWrite(myFile);
WriteLn(myFile, FormatDateTime('yyyy-mmm-dd hh:mm:ss',now)+ ' '+ cMsg);
CloseFile(myFile);
except
end;
end;
end.