Einzelnen Beitrag anzeigen

Trafel21

Registriert seit: 16. Nov 2018
69 Beiträge
 
#3

AW: Windows Service Startet nicht

  Alt 9. Jul 2019, 11:01
Vielen Dank, funktioniert.

Ich hatte davor mittels TThread versucht ein Thread zu handeln.

Code:
unit MyService;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
  AsteriskDataModul, Eventlog;
type
  TWorkflowThread = class(TThread)
  protected
    procedure Execute; override;
    var AsteriskCallControlModule: Tbc_Asterisk_CallControl;
  end;

  TBalyTelephoneControl = class(TService)
    procedure ServiceAfterInstall(Sender: TService);
    procedure ServiceExecute(Sender: TService);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceShutdown(Sender: TService);
  private
    { Private-Deklarationen }
    fWorkflowThread: TWorkflowThread;
  public
    function GetServiceController: TServiceController; override;
    { Public-Deklarationen }
  end;

var
  BalyTelephoneControl: TBalyTelephoneControl;

implementation

{$R *.dfm}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  BalyTelephoneControl.Controller(CtrlCode);
end;

function TBalyTelephoneControl.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TBalyTelephoneControl.ServiceAfterInstall(Sender: TService);
begin
  LogMessage('Afterinstall');
end;

procedure TBalyTelephoneControl.ServiceExecute(Sender: TService);
begin
  ServiceThread.ProcessRequests(true);
end;

procedure TBalyTelephoneControl.ServiceShutdown(Sender: TService);
begin
  if Assigned(fWorkflowThread) then
  begin
    fWorkflowThread.Terminate;
    while WaitForSingleObject(fWorkflowThread.Handle, WaitHint-100) = WAIT_TIMEOUT do
      ReportStatus;
    FreeAndNil(fWorkflowThread);
  end;
end;

procedure TBalyTelephoneControl.ServiceStart(Sender: TService; var Started: Boolean);
begin
  Eventlog.EventLog_Success('Service Start','Service Start','');
  fWorkflowThread := TWorkflowThread.Create(False);
  Started := True;
end;

procedure TBalyTelephoneControl.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
  Eventlog.EventLog_Success('Service Stop','Service Stop','');
  ServiceShutdown(Sender);
  Stopped := True;
end;

{ TWorkflowThread }

procedure TWorkflowThread.Execute;
begin
  inherited;
  try
    if not(assigned(AsteriskCallControlModule)) then
    begin
      AsteriskCallControlModule := Tbc_Asterisk_CallControl.Create(nil);
    end;
    while not Terminated do
    begin
      Sleep(500);
    end;
      FreeAndNil(AsteriskCallControlModule);
  except
    on e: Exception do Eventlog.EventLog_Error('Error',e.Message,'')
  end;
end;


end.
Muss ich nicht im ServiceThread mein Datamodul irgendwie einbinden?

Geändert von Trafel21 ( 9. Jul 2019 um 11:04 Uhr)
  Mit Zitat antworten Zitat