是否可以安装同一个 delphi 服务应用程序的多个实例? [英] Is it possible to install multiple instances of the same delphi service application?

查看:35
本文介绍了是否可以安装同一个 delphi 服务应用程序的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Delphi 构建的服务应用程序,效果很好.它完全符合我的要求,而且一切都很愉快.一切都很好,直到我想在一台机器上运行该服务的两个(或更多)实例.由于服务名称被硬编码到程序中(通过服务的 Name 属性),我只能在任何给定的计算机上安装该服务一次.如果我尝试在运行时修改 Name 属性,除非 Name 属性设置为与设计时设置的相同,否则服务不会响应.

I have a service application built in Delphi that works great. It does exactly what I want it to do and all is happy. All is fine until I want to run two (or more) instances of that service on a single machine. Since the service name is hard coded into the program (via the Name property of the service), I can only install the service once on any given computer. If I try to modify the Name property at run-time, the service does not respond unless the Name property is set to the same thing that was set during design time.

我为此做了一个解决方法,我将所有不直接与服务控制管理器交互的代码封装到单独的单元中.然后,我为每个我想要的服务实例编写一个单独的 Delphi 项目,该服务的代码刚好足以启动自身并开始运行主代码.

I have done a workaround for this where I have all of the code that is not interacting directly with the service control manager encapsulated out into separate unit(s). Then I write a separate Delphi project for each instance that I want of the service that has just enough code to launch itself and start running the main code.

在我看来,这种方法很丑陋,而且效率低下.它适用于两个实例,但随后我们需要第三个和第四个......

This method is, in my opinion, ugly and is certainly inefficient. It works okay for two instances, but then we need a third and a fourth and ...

有什么方法可以修改我的代码,以便我只有一个 Delphi 项目,它可以通过一些简单的运行时输入(例如命令行标志)将自身作为多个服务实例安装和运行?

Is there any way that I can modify my code so that I have just one Delphi project that can install and run itself as multiple service instances with some simple run-time input (e.g. command line flag)?

或者更广泛的问题:是否有实现目标的正确方法"?

Or perhaps a broader question: Is there a "right way" to accomplish goal?

推荐答案

您还没有说明您尝试在 TService 子类中更改什么.

You haven't made it clear what you have tried to change in the TService subclass.

您是否添加了BeforeInstall"处理程序?

Have you added a "BeforeInstall" handler?

类似于:

procedure TServiceMain.ServiceLoadInfo(Sender : TObject);// new method, not an override
begin
  Name := ParamStr(2);
  DisplayName := ParamStr(3);
end;

procedure TServiceMain.ServiceBeforeInstall(Sender: TService);
begin
  ServiceLoadInfo(Self);
end;
procedure TServiceMain.ServiceCreate(Sender: TObject);
begin
  ServiceLoadInfo(Self);
end;

如果您经常这样做,请子类化 TService 以在构造函数中执行此操作.

If you do this regularly, subclass TService to do thie in the Constructor instead.

您也应该在 BeforeUninstall 中执行相同的操作 - 将两个事件指向相同的方法.

You should do the same in the BeforeUninstall as well - point both events at the same method.

C:>servicename /install MyService "My Service Description"

这篇关于是否可以安装同一个 delphi 服务应用程序的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆