如何让我的Windows服务重新启动? [英] How do I get my Windows Service to restart itself?

查看:321
本文介绍了如何让我的Windows服务重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的Windows服务项目,在运行时,需要在每次更新自己的资源(DLL等)后重新启动。

I created a new Windows Service project which, when running, needs to be restarted after every time it updated it's own resources (DLL's etc).

我可以让我的Windows服务重启本身? 

Can I get my Windows Service to restart itself? 

我真的希望没有必要创建第二个应用程序/服务来重新启动。 

I really hope that it won't be necessary to create a second application/service to do the restart. 

问候 

推荐答案

您是否尝试过:

Have you tried:

Application.Restart();

我知道它会导致正常的winforms应用程序终止并重新启动,但我不确定它是否适用于Windows服务。尝试的东西......

I know it will cause a normal winforms app to terminate and restart, but I'm not sure if it'll work for a Windows service. Something to try though...


让Windows服务重启的最佳方法是设置两件事:



1)转到服务并查看已安装服务的属性。转到恢复并将首次失败选择列表设置为重新启动服务。如果您希望能够在一天内多次重新启动,请将 后的重置失败计数文本框设置为0。将重新启动服务文本框设置为您要等待的时间。现在,如果您的服务因任何原因失败,它将尝试重新启动。



2)接下来在您的Windows服务中,您将其设置为失败。退出错误代码应该这样做。 ( ExitCode ref)



现在你的Windows服务会告诉服务提供商它失败并将重新启动。



为什么有人这样做,有些人可能会问?如果没有通过安装程序安装/更新它们,那么性能计数器就是一个很好的例子。性能计数器需要时间来配置。因此,Windows服务可以即时创建它们,重新启动,然后使用它们。除了这个例子,我确定还有其他原因。


步骤1可以替换为运行程序甚至重新启动计算机。我更喜欢运行一个向我报告错误的程序,然后如果X失败计数小于Y则重新启动服务。我在后续失败中运行我的程序。



最后,您可以在安装服务期间配置恢复设置。它上面有一个 CodeProject文章
Best way to have a Windows service restart itself is to set up two things:

1) Go to Services and look at the properties of your installed service. Go to Recovery and set the First Failure pick list to "Restart the Service". Set the Reset fail count after text box to 0 if you want to be able to restart it multiple times in a day. Set the Restart service after text box to how long you want to wait. Now if your service fails for whatever reason, it will try to restart.

2) Next in your Windows service, you set it to fail. Exiting with an error code should do it. (ExitCode ref)

Now your Windows service will tell the service provider that it failed and will restart.

Why would anyone do this, some may ask? Well performance counters is a good example if one didn't install/update them via an installation program. Performance counters need time to configure. So a Windows service can create them on-the-fly, restart, and then use them. Other than that example, I'm sure there are other reasons.

Step 1 can be replaced with running a program or even restarting the computer. I prefer to run a program that reports the error to me and then restarts the service if X number of fail counts is less than Y. I run my program in the subsequent failures.

Lastly you can configure the Recovery settings during installation of your service. There is a CodeProject article on it.


某人这里建议安排任务以启动服务(例如,将来30秒),然后让服务停止。
Someone here suggested scheduling a task to have the service start (say, in 30 seconds in the future), then have the service stop itself.


这篇关于如何让我的Windows服务重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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