C# 如何每 10 分钟在服务内运行一次函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10206000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to have a function run inside a service every 10 minutes?
提问by Beginner
I have a windows service running, inside this i want to run a function every then minutes. I have found some code but it doesn't seem to work? I have a logger and it does not seem to go into the timer_Elapsed function ever?
我有一个 Windows 服务正在运行,在此我想每隔几分钟运行一个函数。我找到了一些代码,但它似乎不起作用?我有一个记录器,它似乎从未进入 timer_Elapsed 函数?
protected override void OnStart(string[] args)
{
// SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
// test.Import();
log.Info("Info - Service Started");
_timer = new Timer(10 * 60 * 1000); // every 10 minutes??
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
log.Info("Info - Check time");
DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48);
if (_lastRun < startAt && DateTime.Now >= startAt)
{
// stop the timer
_timer.Stop();
try
{
log.Info("Info - Import");
SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
test.Import();
}
catch (Exception ex) {
log.Error("This is my error - ", ex);
}
_lastRun = DateTime.Now;
_timer.Start();
}
}
采纳答案by Daniel Hilgarth
You need to start the timer:
您需要启动计时器:
protected override void OnStart(string[] args)
{
log.Info("Info - Service Started");
_timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start(); // <- important
}
回答by Tigran
I don't see _timer.Start(), that should be your problem.
我不明白_timer.Start(),那应该是你的问题。
回答by FIre Panda
Try starting the timer,
尝试启动timer,
protected override void OnStart(string[] args)
{
// SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
// test.Import();
log.Info("Info - Service Started");
_timer = new Timer(10 * 60 * 1000); // every 10 minutes??
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
log.Info("Info - Check time");
DateTime startAt = DateTime.Today.AddHours(9).AddMinutes(48);
if (_lastRun < startAt && DateTime.Now >= startAt)
{
// stop the timer
_timer.Stop();
try
{
log.Info("Info - Import");
SmartImportService.WebService.WebServiceSoapClient test = new WebService.WebServiceSoapClient();
test.Import();
}
catch (Exception ex) {
log.Error("This is my error - ", ex);
}
_lastRun = DateTime.Now;
_timer.Start();
}
}
回答by Reed Copsey
Daniel Hilgarthis correct - the main issue is that you never call Start on the timer.
Daniel Hilgarth是正确的 - 主要问题是你从来没有在计时器上调用 Start。
That being said, you might want to also consider using the Windows Task Scheduler instead of a service with a timer. This allows you to schedule the task to run every 10 minutes, but also change the schedule whenever desired without a compilation change.
话虽如此,您可能还想考虑使用 Windows 任务计划程序而不是带有计时器的服务。这允许您将任务安排为每 10 分钟运行一次,但也可以在不更改编译的情况下随时更改安排。
回答by swdev
I need this functionality also. That is, my C# windows service must check email every 10 minutes. I stripped down some logic to make the code more effective, as follows :
我也需要这个功能。也就是说,我的 C# windows 服务必须每 10 分钟检查一次电子邮件。我剥离了一些逻辑以使代码更有效,如下:
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_timer.Stop();
try
{
EventLog.WriteEntry(Program.EventLogName, "Checking emails " + _count++);
}
catch (Exception ex)
{
EventLog.WriteEntry(Program.EventLogName, "This is my error " + ex.Message);
}
_timer.Start();
}
The timer_elapsed method indeed will be call every 10 minutes, starting from the first _timer.start(), which you miss it by the way. I haven't done any checking of the _lastRun and startAt. I don't think we need it
timer_elapsed 方法确实会每 10 分钟调用一次,从第一个 _timer.start() 开始,顺便说一下,您错过了它。我还没有对 _lastRun 和 startAt 进行任何检查。我认为我们不需要它

