如何发送自定义命令一个.net窗口服务from.Net code?自定义、命令、窗口、Net

由网友(喂你一口甜甜)分享简介:如下面的链接,就可以使用C#code停止,启动,和停止,然后启动的服务。As in the following link, one can stop, start, and "stop, then start" a service using C# code.http://www.csharp-examples.ne...

如下面的链接,就可以使用C#code停止,启动,和停止,然后启动的服务。

As in the following link, one can stop, start, and "stop, then start" a service using C# code.

http://www.csharp-examples.net/restart-windows-service/

我已经出炉,它实现的OnStart 的onStop A的.Net服务。不过,我需要实现一种智能启动功能,这不仅仅是停止,然后开始更多地参与。我需要保持的停机时间只有几秒钟,如果是(但停止+启动可在这种情况下,当干净的完成需要几分钟,我必须干净做到这一点),并在现有的系统的某些部分,而其他的重启/刷新

I have baked a .Net service that does implement OnStart and OnStop. However, I need to implement a "smart restart" functionality which is more involved than just stopping and then starting. I need to keep the downtime to just a few seconds if that (but Stop + Start can take minutes in this case when done cleanly, and I must do it cleanly), and having some parts of the system available while others are rebooting/refreshing.

长话短说 - 之前,我跳进执行本 OnSmartRestart 的功能,我想确保有一个相当简单的方法来调用从其他C#应用程序,这个调用

Long story short - before I jump into implementing this OnSmartRestart functionality, I want to make sure that there is a fairly easy way to invoke this call from a other C# app.

此功能是很重要的,但危险的。我希望把它相当隐蔽,有些安全,同时保持它的简单,并确保这对我的Windows服务时对其进行定期的琐事,而不是重新启动的性能影响甚微。如果我有轮询某些文件或打开一个端口,花费太多的CPU上这样做,它不会是一个很好的解决方案。我也想保持这种尽可能简单(对不起重复)。

This feature is important, but dangerous. I want to make it fairly hidden, somewhat secure, also keep it simple, and make sure that this has negligible effect on the performance of my Windows Service while it is performing its regular chores and not rebooting. If I have to poll for some file or open a port and spend too much CPU on doing that, it would not be a good solution. I also want to keep this AS SIMPLE AS POSSIBLE (sorry for repetition).

推荐答案

您可以发送命令,以使用服务ServiceController.ExecuteCommand:

You can send "commands" to a service using ServiceController.ExecuteCommand:

const int SmartRestart = 222;

service.ExecuteCommand(SmartRestart);
service.WaitForStatus(ServiceControllerStatus.Running, timeout);

该服务可以通过覆盖ServiceBase.OnCustomCommand:

protected override void OnCustomCommand(int command)
{
    if (command == SmartRestart)
    {
        // ...
    }
}
阅读全文

相关推荐

最新文章