是否可以安装同一个 delphi 服务应用程序的多个实例?

编程入门 行业动态 更新时间:2024-10-26 07:38:20
本文介绍了是否可以安装同一个 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 服务应用程序的多个实例?

本文发布于:2023-11-16 02:40:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   应用程序   实例   delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!