我需要帮助。 我有Windows服务,我需要在特定时间每小时运行一次这项服务,例如:09:05,10:05,11:05,.... 我的服务现在每小时开始,但从我开始这项服务的每个小时开始。 那么我怎样才能实现我的需求呢。
我的代码:
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.WriteToFile("Starting Service {0}");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
this.WriteToFile("Stopping Service {0}");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
this.WriteToFile(" interval start {0}");
} }
答案 0 :(得分:6)
您应该检查每个' n'来自计时器的秒数(例如1):
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.WriteToFile("Starting Service {0}");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 1000; // 1000 ms => 1 second
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
this.WriteToFile("Stopping Service {0}");
}
private int lastHour = -1;
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
var curTime = DateTime.Now; // Get current time
if (lastHour != curTime.Hour && curTime.Minute == 5) // If now 5 min of any hour
{
lastHour = curTime.Hour;
// Some action
this.WriteToFile(" interval start {0}");
}
}
}
答案 1 :(得分:2)
以下是另一种计算第一个间隔的方法:
var minutesAfterHourToStart = 5; // Start at 5 minutes after the hour
var now = DateTime.Now;
var minutesToStart = 60 - (now.Minute - (minutesAfterHourToStart - 1));
if (minutesToStart > 60) minutesToStart -= 60;
var secondsToStart = 60 - now.Second + (minutesToStart * 60);
timer.Interval = TimeSpan.FromSeconds(secondsToStart).TotalMilliseconds;
然后,在OnTimeElapsed
事件中,您将设置每小时运行的间隔。为了防止经常反复将变量设置为相同的值,我们可以设置一个我们已设置最终间隔的全局变量:
class MyService
{
private bool resetInterval = true;
然后我们可以检查这个,并在第一次通过时将其设置为false:
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
if (resetInterval)
{
timer.Interval = TimeSpan.FromHours(1).TotalMilliseconds;
resetInterval = false;
}
答案 2 :(得分:0)
你只需要以不同的方式启动计时器:
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.WriteToFile("Starting Service {0}");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
var excess = DateTime.Now.Minute - 5;
var span = DateTime.Now.AddMinutes(excess <= 0 ? -excess : 60- excess) - DateTime.Now;
timer.Interval = span.TotalMilliseconds;
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
this.WriteToFile("Stopping Service {0}");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
var excess = DateTime.Now.Minute - 5;
var span = DateTime.Now.AddMinutes(excess <= 0 ? -excess : 60- excess) - DateTime.Now;
timer.Interval = span.TotalMilliseconds;
this.WriteToFile(" interval start {0}");
}
}
这样你就可以设置第一个&#34; x:05&#34;分钟,然后每小时。
答案 3 :(得分:0)
也许只是保留您的代码,并将您的服务设置为手动启动,然后使用Windows调度程序在特定时刻启动它一次。剩下的工作将做定时器和OnElapse方法; 这可以帮助您:https://stackoverflow.com/a/36309450/5358389