在delphi中创建Windows服务
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个专门为简化服务调试过程而开发的商业解决方案。您可以直接从 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.
如果您创建了一个服务,您可以通过使用参数
/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 enterservices.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.
您不能只从 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.