Link to home
Start Free TrialLog in
Avatar of alpires
alpiresFlag for Brazil

asked on

delphi and TService

Hi Experts

I'm trying to create a service in delphi, but is not working. I am using delphi 7 and windows xp professional.

What's wrong with this code

Alexandre
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  ExtCtrls;
 
type
  TService1 = class(TService)
    Timer1: TTimer;
    procedure ServiceExecute(Sender: TService);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;
 
var
  Service1: TService1;
 
implementation
 
{$R *.DFM}
 
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  Service1.Controller(CtrlCode);
end;
 
function TService1.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;
 
procedure TService1.ServiceExecute(Sender: TService);
begin
  Timer1.Enabled := True;
  while not Terminated do
  ServiceThread.ProcessRequests(True);
  Timer1.Enabled := False;
end;
 
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;
 
end.

Open in new window

Avatar of Geert G
Geert G
Flag of Belgium image

how are you trying to run this ?

you need to run it from the cmd prompt

service.exe /install
Avatar of alpires

ASKER

Yes I'm doing this, but still not working
Avatar of alpires

ASKER

Well, Now I made these changes and worked:

procedure TService1.ServiceExecute(Sender: TService);
begin
  Timer1.Enabled := True;
  while not self.Terminated do
  self.ServiceThread.ProcessRequests(true);
  Timer1.Enabled := False;
end;

But now i have some questions:

1- How star the service by another delphi
ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to start it from the Delphi Ide :

Add "/Install" to the Parameters Edit in Menu Run / Parameters / Tab Local

And hit Run (F9)