在特定时间启动已部署的Windows服务 [英] Start a windows service, which is already deployed, at a specific time

查看:70
本文介绍了在特定时间启动已部署的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个已经部署的Windows服务(使用安装和部署项目).
我需要在特定时间(例如每天早上6:00)启动此服务.而且它应该继续运行直到固定的时间(例如,当天下午7:00).
该服务应在结束时间停止,并应在第二天的计划开始时间重新启动.
有人可以帮我一些代码以编程方式(在规定的时间)启动服务吗?

在此先感谢您!

Hi,

I have a windows service which is already deployed (using the setup and deployment project).
I need to start this service at a specific time (e.g. 6:00 a.m. everyday). And it should continue running till a fixed time (e.g. 7:00 p.m. same day).
The service should be stopped at the end time and should restart at the scheduled start time the next day.
Can anybody help me with some code to start the service programmatically(at the stipulated time)?

Thanks in advance!

推荐答案

实际上,您只需要计划的过程,而不是服务.仍然检查链接是否有帮助.

.NET Windows服务计划程序 [ http://social.msdn.microsoft.com/论坛/en-US/csharpgeneral/thread/268a194c-747d-40b6-8b9e-903e44e47686 [
Actually you just need a scheduled process, not the service. Still check the links if it helps.

.NET Windows Service Scheduler[^]
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/268a194c-747d-40b6-8b9e-903e44e47686[^]


如果您真的想这样做,并且依靠调度程序,您要做的就是调度两个要运行的命令行.第一个在您指定的开始时间,第二个在您的停止时间:
If you REALLY want to do it this way and rely on a scheduler, all you have to do is schedule two command lines to be run. The first at your specified start time and the second at your stop time:
net start servicename

net stop servicename



不需要代码...



No code required...


更改代码以使服务独立启动和停止.如果您在配置文件中提供开始/停止时间,则只需更改文件即可自定义开始/停止时间.

如果编写得很巧妙,则可以让服务查找对配置文件的更改,如果检测到该更改,则可以更改其计划,而不必由用户停止/重新启动.

编辑===========

为什么要一票呢?这是最可靠的解决方案,因为与编写另一个应用程序来控制服务,然后将其绑定到Windows调度程序相比,活动部件更少.
Change the code to have the service start and stop on its own. If you provide the start/stop time in a config file, you can customize the start/stop time by simply changing the file.

If cleverly written, you can have the service look for changes to the config file, and if detected, alter it''s schedule without having to be stopped/restarted by the user.

EDIT ===========

Why was this one-voted? It''s the most reliable solution because there are fewer moving parts than writing yet another app to control the service, and then tying it to the Windows scheduler.


这篇关于在特定时间启动已部署的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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