Windows服务在Delphi中创建并启动,但没有正常运行



嗨,我已经在Delphi中创建了windows服务。我已经安装并启动了服务。一切正常。甚至我都可以在任务管理器中查看。我的服务是跑步。

但是我包含在OnExecute方法中的代码不工作。

My whole Code:

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.

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

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

这意味着你不能使用你的服务来显示UI。按照设计,对ShowMessage的调用不能在服务中工作。

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

相关内容

最新更新