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

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

问题描述

我有一个服务应用程序内置在Delphi,工程很好。它完全是我想要的,一切都很开心。一直到我想要在一台机器上运行该服务的两个(或更多)实例之前,这一切都很好。由于服务名称被硬编码到程序中(通过服务的Name属性),我只能在任何给定的计算机上安装一次服务。如果我尝试在运行时修改Name属性,则除非将Name属性设置为在设计时设置的相同内容,否则该服务不会响应。



我已经做了一个解决方法,我有所有代码不直接与服务控制管理器封装到单独的单元中进行交互。然后我为每个实例编写一个单独的Delphi项目,我想要的服务只有足够的代码启动自己并开始运行主要代码。



这个方法是,在我看来,丑陋肯定是低效的。对于两个实例,它可以正常工作,但是我们需要第三个和第四个...



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



或者更广泛的问题是:是否有一个正确的方法来完成目标?

解决方案

你还没有清楚你在TService中尝试改变的内容



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



某些东西:

  procedure TServiceMain.ServiceLoadInfo(Sender:TObject); //新方法,而不是覆盖
begin
名称:= ParamStr 2);
DisplayName:= ParamStr(3);
结束

程序TServiceMain.ServiceBeforeInstall(Sender:TService);
begin
ServiceLoadInfo(Self);
结束
procedure TServiceMain.ServiceCreate(Sender:TObject);
begin
ServiceLoadInfo(Self);
结束

如果你经常这样做,子类TService在构造函数中执行thie。



您也应该在BeforeUninstall中执行相同的操作 - 同样的方法将两个事件相结合。

  C:\> servicename / install MyService我的服务说明


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.

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 ...

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?

解决方案

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

Have you added a "BeforeInstall" handler?

Something like:

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;

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

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天全站免登陆