在delphi中创建Windows服务

发布于 2024-11-08 08:14:18 字数 753 浏览 0 评论 0原文

我在delphi中创建了一个windows服务。我的代码在 ServiceExecute 中运行

procedure TMyService.ServiceExecute(Sender: TService);
 while not Terminated do
 begin
      CallSomeProcedure;  
      Sleep(1000);
      ServiceThread.ProcessRequests(false);
 end;
end;

不幸的是,我无法运行此代码。即使在调试时我似乎也没有调用该过程。 Myservice.exe 中的代码如下所示。

begin 
    if not Application.DelayInitialize or Application.Installing then
       Application.Initialize;
    Application.CreateForm(TMyService, MyService);
    Application.Run;
end.

如果我添加到 MyService.exe 中,我可以让 serviceExecute 运行,

MyService.ServiceExecute(nil);

但是如果我将其安装为服务,它似乎没有运行,因为 Application.Run 不执行

任何操作 不确定我做错了什么,但我们将不胜感激。

谢谢

I have created a windows service in delphi. My code is run within ServiceExecute

procedure TMyService.ServiceExecute(Sender: TService);
 while not Terminated do
 begin
      CallSomeProcedure;  
      Sleep(1000);
      ServiceThread.ProcessRequests(false);
 end;
end;

Unfortunately, I can not get this code to run. I doesn't seem to call the procedure even when I'm debugging.
The code in Myservice.exe looks like this.

begin 
    if not Application.DelayInitialize or Application.Installing then
       Application.Initialize;
    Application.CreateForm(TMyService, MyService);
    Application.Run;
end.

I can get the serviceExecute to run if I add

MyService.ServiceExecute(nil);

into MyService.exe however if I install it as a service it appears not to be running as Application.Run does nothing

Not sure what I'm doing wrong, but any help would be much appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住你手 2024-11-15 08:16:50

有一个专门为简化服务调试过程而开发的商业解决方案。您可以直接从 Delphi IDE 调试您的代码,包括服务的OnStart 事件。

There is commercial solution that was developed specifically to simplify service debugging process. You can debug your code directly from the Delphi IDE, including service's OnStart event.

几度春秋 2024-11-15 08:15:55

如果您创建了一个服务,您可以通过使用参数 /install 运行它来安装它。

之后,该服务应该显示在您的其他服务之间(转到 start/run/代码>并输入services.msc)。

然后您可以启动它,并且可以通过附加调试器来调试它。

然而,这样的工作是痛苦的。我不敢相信有些人实际上是这样工作的。我通常将所有业务逻辑放在单独的单元中,我可以从“普通”应用程序运行它们。只有当效果良好时,我才会将其包装在服务中并尝试。

有时我什至创建一个既可以作为服务运行也可以通过 GUI 运行的应用程序。您可以简单地实例化您的服务类。你只需要自己启动它,但是调试起来会容易很多。

If you've created a service, you can install it by running it with parameter /install

After that, the service should show up between your other services (go to start/run/ and enter services.msc).

You can then start it, and you can debug it by attaching the debugger to it.

However, it's painful to work like that. I can't believe that some people actually work like that. I usually have all my business logic in separate units that I can run from a "normal" application. Only when that works out good, I wrap it in a service and try that.

Sometimes I even create an application that can run both as a service or with a GUI. You can simply instantiate your service class. You just need to start it yourself, but it'll be a lot easier to debug.

酒绊 2024-11-15 08:15:21

您不能只从 IDE 运行服务来调试它;在这种情况下它就会退出。该服务必须由服务控制管理器启动。另外,您不应该直接调用 ServiceExecute。

以下是如何调试服务的文档

You cannot just run the service from the IDE to debug it; in that case it will just exit. The service has to be started by the service control manager. Also, you shouldn't be calling ServiceExecute directly.

Here's documentation how to debug services.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文