运行定期Windows服务 [英] Windows Service that runs Periodically

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

问题描述

我在写,一旦启动将运行每X小时窗口服务。在完成这个过程是相当密集的,所以我想用一个后台工作。我使用一个设置文件来存储和运行​​上一次服务运行之间的时间

I'm writing a windows service that once started will run every X hours. The process it completes is fairly intensive, so I want to use a background worker. I'm using a Settings file to store both the hours between runs and the last time the service ran.

我不太确定最好的方式做到这一点 - 那就是我想要的服务,使用尽可能少的资源,尽可能为空闲,并且当它运行时,它需要在后台工作运行,报告它的所作所为,然后返回到空闲模式。

I'm not exactly sure the best way to do this - that is, I want the service to idle using as few resources as possible, and when it runs, it needs to run in the background worker, report what it did, and then go back into idle mode.

我已经想过使用2背景工人。第一个工人将是服务的私有局部变量在运行这样的:

I've thought about using 2 background workers. The first worker would be a private local variable for the service that runs something like this:

while (true)
{
      //create new background worker and run

      Thread.Sleep(Settings.Default.SleepTimeHours * 3600000);
}



与创建循环的每个迭代,并毁坏了,当子工人完成。为了支持取消,我想我会在服务中可用的第二工人的本地实例,但如果进程当前没有运行它会为空。当辅助工人完成,它会发出我的报告,请在设置文件中的最后一次运行时间,然后处置工人和设置为null引用。

with a sub worker that is created each iteration of the loop, and destroyed when completed. To support cancellation, I think I would have to have a local instance of the second worker available in the service, but it will be null if the process currently is not running. When the secondary worker completes, it would send out my reporting, set the last run time in the settings file, and then dispose of the worker and set the reference to null.

我不知道是否有更好的方法来做到这一点还是一个最佳实践。

I'm wondering if there is a better way to do this or a best practice.

感谢

推荐答案

我通常使用一个定时器,那么当这个过程开始运行停止。

I typically use a Timer, then stop it when the process starts to run.

下面的解释的how做的。

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

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