将每小时运行的Windows服务 [英] Windows service that will run every hour

查看:205
本文介绍了将每小时运行的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以建立一个windows服务并安装它。

I m able to build a windows service and install it.

我很好奇我怎么能每小时运行这个服务?我希望它每隔一小时定期运行。

I m curious how can i run this service every hour ? I want it to run every hour periodically.

我还需要知道它运行的小时范围,以便我可以将它存储在某个地方。

I also need to know the hour range that it s running so that I can store it somewhere.

我该如何做?

编辑:此服务将安装在许多机器上,我不想在100台服务器上创建一个计划任务。

Edit : This service will be installed on many machines, therefore, I dont want to create a scheduled task say on 100 servers.

推荐答案

如果您希望任务按固定间隔运行反对不断,您应该使用任务计划程序

If you want a task to run on a regular interval as opposed to constantly, you should look into using the Task Scheduler.

如果你需要你的代码是一个服务,但要每隔一小时激活,最简单的方法是使你的服务为COM对象并有一个简单的任务计划每小时调用一个jscript / vbscript创建你的COM对象,并调用简单的方法。

If you need your code to be a service, but to be "activated" every hour, the easiest approach would be to make your service a COM object and have a simple task scheduled every hour that invokes a jscript/vbscript that creates your COM object and calls simple method on it.

另一种方法是使用任何等待API可以浪费一个小时而不消耗周期。

The alternative is to use any of the wait APIs to "waste" an hour without consuming cycles.

请注意,您还必须考虑一些有趣的设计决策,这取决于您的场景:

Note that you also have to consider some interesting design decisions that depend on what your scenario is:


  • 如果您的服务崩溃或被用户停止,您的服务如何启动?

  • 超过一小时后,您应该再次运行还是需要等待确切的小时计划?

  • 如果时区如何,您如何跟踪最后的激活时间

  • 您的服务是否阻止计算机在空闲时或笔记本电脑外盖关闭时进入睡眠/休眠状态?

  • how is your service going to be started if it crashes or is stopped by the user?
  • if you are started after more than an hour, should you run again or do you need to wait to get on the exact hourly schedule?
  • how do you keep track of the last "activation" time if the timezone or the day-light saving time has changed while you were not active?
  • does your service prevent the computer from going to sleep/hibernate on idling or when the laptop cover is closed? if not, do you need to awake the computer on the hour to get your service working on your schedule?

其中一些被采取了,如果没有,你需要在每小时唤醒电脑,照顾任务调度程序,因此我强烈建议您在代码中等待一小时。

Some of those are taken care of by the task scheduler, so I would strongly recommend going that route vs. waiting for an hour in your code.

这篇关于将每小时运行的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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