title: Service application in Delphi
body: <p>I am struggling with service application in Delphi but no major success so far. I tried to recreate <a href="http://www.devarticles.com/c/a/Delphi-Kylix/Creating-a-Windows-Service-in-Delphi/4/" rel="nofollow">this project</a>, but it doesn't seem to work properly. File is created, but date and time aren't added to file every 10 seconds. I also don't see a message popping up from my ShowMessage. I successfully install and start service application.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>unit TMS;
  
  interface
  
  uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
    Vcl.ExtCtrls;
  
  type
    TWorkflow = class(TService)
      Timer1: TTimer;
      procedure ServiceExecute(Sender: TService);
      procedure Timer1Timer(Sender: TObject);
      procedure ServiceBeforeInstall(Sender: TService);
    private
      { Private declarations }
    public
      function GetServiceController: TServiceController; override;
      { Public declarations }
    end;
  
  var
    Workflow: TWorkflow;
  
  implementation
  
  {$R *.dfm}
  
  procedure ServiceController(CtrlCode: DWord); stdcall;
  begin
    Workflow.Controller(CtrlCode);
  end;
  
  function TWorkflow.GetServiceController: TServiceController;
  begin
    Result := ServiceController;
  end;
  
  procedure TWorkflow.ServiceBeforeInstall(Sender: TService);
  begin
    Interactive := True;
  end;
  
  procedure TWorkflow.ServiceExecute(Sender: TService);
  begin
    while not Terminated do
    begin
      ServiceThread.ProcessRequests(True);
    end;
  end;
  
  procedure TWorkflow.Timer1Timer(Sender: TObject);
  const
    FileName = 'D:\Projekti\EMBRACADERO\TMSWorkflow\Win32\Debug\Log.txt';
  var
    F : TextFile;
  begin
    AssignFile(F, FileName);
    if FileExists(FileName) then
      Append(F)
    else
      Rewrite(F);
    Writeln(F, DateTimeToStr(Now), ' ', DiskFree(0));
    ShowMessage(DateTimeToStr(Now));
    CloseFile(F);
  end;
  
  end.
  </code></pre>
  
  <p>Could somebody give me an example of a service application with threads maybe or service with visual components included?</p>
  
  <p>UPDATE1:</p>
  
  <p>It is working with following code for inserting some data in database every 3 seconds.</p>
  
  <pre><code>private
      thread : TThread;  
  
  procedure TWorkflow.InsertInDatabase;
  begin
    FDTransaction1.StartTransaction;
    try
      FDQuery1.Execute;
      FDTransaction1.Commit;
    except
      FDTransaction1.Rollback;
    end;
  end;
  
  procedure TWorkflow.ServiceExecute(Sender: TService);
  begin
    while not Terminated do
    begin
      ServiceThread.ProcessRequests(False);
      InsertInDatabase();
      thread.sleep(3000);
    end;
  end;
  
  procedure TWorkflow.ServiceStart(Sender: TService; var Started: Boolean);
  begin
    thread := TThread.Create;
  end;
  
  procedure TWorkflow.ServiceStop(Sender: TService; var Stopped: Boolean);
  begin
    FreeAndNil(thread);
  end;
  </code></pre>