是否ServiceBase.Run(ServiceBase [])方法中运行的所有服务在阵列异步? [英] Does ServiceBase.Run(ServiceBase[] ) method run all services in array asynchronously?

查看:276
本文介绍了是否ServiceBase.Run(ServiceBase [])方法中运行的所有服务在阵列异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ServiceBase [] servicesToRun; 
servicesToRun =新ServiceBase []
{
新服务1(),
新服务2()
};
ServiceBase.Run(servicesToRun);



如何将这项工作?将服务1上运行,并在其的OnStart()完成后,客服2的的OnStart()将运行,或两者兼而有之他们将在同一时间运行?
。如果是那样的话,我明确希望服务1的的OnStart 运行 Service2.OnStart()之前完成,我怎么会去



这会做的伎俩:

  ServiceBase.Run(新服务1())
ServiceBase.Run(新服务2())


解决方案

您是误会什么 ServiceBase.Run()实际上做。



文档:




注册与服务控制管理器(SCM)多种业务的可执行文件。




顺序是这样的:




  • 系统开始请求收到一个(或者两个)服务

  • 的SCM启动该可执行文件。

  • 您的主函数调用ServiceBase.Run()。

  • 单片机调用的OnStart()匹配启动请求,在一个新的线程服务。的除非这两个服务正在启动,的OnStart()将不可以堪称为另一个!



如果这两种服务的的被启动然后在其中的OnStart()被调用是不确定的,除非一个服务已被配置为具有对其他的依赖性的顺序。配置这种依赖性是唯一安全的方法,以确保特定的顺序,任何弄虚作假,如在一个的OnStart()从另外一个等待信号可能会导致SCM死锁。



如果只有一个服务启动的,后来在其他服务启动时,SCM将调用的OnStart(),用于在该点的第二服务



注意,你只能叫 ServiceBase.Run()一次(可执行文件有望尽快退出,一旦它返回),这样你的第二个代码段肯定是错误的;该代码将防止服务2从所有运行



此外,注意,如果两个服务不能彼此独立地运行,有可能是没有意义的,在具有两个服务首位。我能想到的地方将是明智的唯一情况是,如果你希望用户能够停止和启动服务2而离开服务1运行。


ServiceBase[] servicesToRun;
            servicesToRun = new ServiceBase[]
            {
                new Service1(),
                new Service2()
            };
            ServiceBase.Run(servicesToRun);

How will this work? Will Service1 be run, and when its OnStart() is done, Service2's OnStart() will be run, or both of them will run at the same time? If that's the case, and I explicitly want Service1's OnStart to be done before running Service2.OnStart(), how would I go?

Would this do the trick:

ServiceBase.Run(new Service1())
ServiceBase.Run(new Service2())

解决方案

You are misunderstanding what ServiceBase.Run() actually does.

From the documentation:

Registers the executable for multiple services with the Service Control Manager (SCM).

The sequence goes like this:

  • A start request is received for one (or perhaps both) services.
  • The SCM launches the executable.
  • Your main function calls ServiceBase.Run().
  • The SCM calls OnStart() for the service matching the start request, in a new thread. Unless both services are being started, OnStart() will not be called for the other one!

If both services are being started then the order in which OnStart() is called is indeterminate, unless one service has been configured to have a dependency on the other. Configuring such a dependency is the only safe way to ensure a particular ordering, any trickery such as waiting in one OnStart() for a signal from the other one may cause the SCM to deadlock.

If only one service was started, and later on the other service is started, the SCM will call the OnStart() for the second service at that point.

Note that you can only call ServiceBase.Run() once (your executable is expected to exit promptly once it returns) so your second code segment is definitely wrong; that code would prevent Service2 from running at all.

Also, note that if the two services cannot run independently of one another, there is probably no point in having two services in the first place. The only case I could think of where it would be sensible is if you want the user to be able to stop and start Service2 while leaving Service1 running.

这篇关于是否ServiceBase.Run(ServiceBase [])方法中运行的所有服务在阵列异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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