Windows服务在Delphi中创建并启动,bot无法正常运行

vin*_*r r 0 delphi service

您好我在Delphi中创建了Windows服务.我已经安装并启动了服务.每件事都很好.即使我可以在任务管理器中检查它.我的服务正在运行.

但是我在OnExecute方法中包含的代码不起作用.

我的整个代码:

unit MyService;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
  Vcl.ExtCtrls,Jira;

type
  TJiraTestlink = class(TService)
    procedure ServiceExecute(Sender: TService);

  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  JiraTestlink: TJiraTestlink;

implementation

{$R *.DFM}

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

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

procedure TJiraTestlink.ServiceExecute(Sender: TService);
const
  SecBetweenRuns = 10;
var
  Count: Integer;
begin
  Count := 0;
  while not Terminated do
  begin
    Inc(Count);
    if Count >= SecBetweenRuns then
    begin
      Count := 0;

      { place your service code here }
      { this is where the action happens }
      ShowMessage(DateTimeToStr(Now));

    end;
    Sleep(1000);
    ServiceThread.ProcessRequests(False);
  end;
end;
end.
Run Code Online (Sandbox Code Playgroud)

我不确定我错在哪里.任何帮助将不胜感激.谢谢.

Dav*_*nan 7

自Vista以来,服务被隔离并在会话0中运行,这是一个非交互式会话.交互式进程在不同的会话中运行,从第一个登录用户的会话1开始.

这意味着您无法使用您的服务来显示UI.ShowMessage通过设计,在服务中呼叫无法工作.

您需要找到一些其他方法来调试您的服务.例如,将消息记录到文本文件中.或者OutputDebugString像SysInternals DebugView这样的查看器能够从不同的会话中捕获这些调试字符串.