Windows服务每隔一小时运行一次 [英] windows service to run every one hour
本文介绍了Windows服务每隔一小时运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个应该每隔一小时运行一次的Windows服务,但它在启动时和完成工作后调用该功能它的说法在1小时后再次开始,它从不调用该函数。
我把下面的代码,请建议
Hi everyone,
I have a windows service which should run every one hour, but its calling the function at the time of starting and after finishing the work only its says started and again after 1 hour it never call the function.
I put the code below, please suggest
public PosNetService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
startservice();
}
private void startservice()
{
checkstatus();
}
private void checkstatus()
{
try
{
System.Threading.Thread.Sleep(1000 * 60 * 60);
// then remaining work to do
推荐答案
使用System.Timers.Timer
您有更多选项,其中System.Threading.Timer
是一个轻量级计时器。我建议你使用System.Timers.Timer
试试这个...
WithSystem.Timers.Timer
you have more options, whereSystem.Threading.Timer
is a lightweight timer. I would recommend you to useSystem.Timers.Timer
Try this ...
using System.Timers;
Timer tmrExecutor = new Timer();
protected override void OnStart(string[] args)
{
tmrExecutor.Elapsed += new ElapsedEventHandler(tmrExecutor_Elapsed); // adding Event
tmrExecutor.Interval = 5000; // Set your time here
tmrExecutor.Enabled = true;
tmrExecutor.Start();
}
private void tmrExecutor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Do your work here
}
protected override void OnStop()
{
tmrExecutor.Enabled = false;
}
这篇关于Windows服务每隔一小时运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文