Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每24小时定时一次_C#_Timer_Windows Services - Fatal编程技术网

C# 每24小时定时一次

C# 每24小时定时一次,c#,timer,windows-services,C#,Timer,Windows Services,我正在学习如何创建一个Windows服务,在我的web服务器上自动发送电子邮件。我已经完成了教程,但是,示例代码每60分钟执行一次服务,相反,我希望服务每天执行一次,每24小时执行一次,比如每天早上9点 下面是示例代码 private Timer scheduleTimer = null; private DateTime lastRun; private bool flag; public StarEmailService() { In

我正在学习如何创建一个Windows服务,在我的web服务器上自动发送电子邮件。我已经完成了教程,但是,示例代码每60分钟执行一次服务,相反,我希望服务每天执行一次,每24小时执行一次,比如每天早上9点

下面是示例代码

    private Timer scheduleTimer = null;
    private DateTime lastRun;
    private bool flag;

    public StarEmailService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("EmailSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource("EmailSource", "EmailLog");
        }
        eventLogEmail.Source = "EmailSource";
        eventLogEmail.Log = "EmailLog";

        scheduleTimer = new Timer();
        scheduleTimer.Interval = 1 * 5 * 60 * 1000;
        scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);

    }

    protected override void OnStart(string[] args)
    {
        flag = true;
        lastRun = DateTime.Now;
        scheduleTimer.Start();
        eventLogEmail.WriteEntry("Started");
    }

    protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (flag == true)
        {
            ServiceEmailMethod();
            lastRun = DateTime.Now;
            flag = false;
        }
        else if (flag == false)
        {
            if (lastRun.Date < DateTime.Now.Date)
            {
                ServiceEmailMethod();
            }
        }
    }
private Timer scheduleTimer=null;
私人日期时间最后运行;
私人布尔旗;
公共StarEmailService()
{
初始化组件();
如果(!System.Diagnostics.EventLog.Source存在(“EmailSource”))
{
System.Diagnostics.EventLog.CreateEventSource(“EmailSource”、“EmailLog”);
}
eventLogEmail.Source=“EmailSource”;
eventLogEmail.Log=“EmailLog”;
scheduleTimer=新计时器();
scheduleTimer.Interval=1*5*60*1000;
scheduleTimer.Appeased+=新的ElapsedEventHandler(scheduleTimer_Appeased);
}
启动时受保护的覆盖无效(字符串[]args)
{
flag=true;
lastRun=DateTime.Now;
scheduleTimer.Start();
eventLogEmail.WriteEntry(“已启动”);
}
受保护的void scheduleTimer_已过期(对象发送方,ElapsedEventArgs e)
{
如果(标志==真)
{
ServiceEmailMethod();
lastRun=DateTime.Now;
flag=false;
}
else if(标志==false)
{
if(lastRun.Date
scheduleTimer.Interval=1*5*60*1000
似乎是将间隔设置为60分钟的代码,但是,我不确定需要对其进行哪些修改,以使其在上午9点每24小时运行一次

如有任何建议,将不胜感激


谢谢。

您有两种选择:

  • 使用
  • 使用Windows计划任务

不要依赖其他计时器,因为它们在不久的将来将失去同步。

最好将计时器设置为较小的间隔,并检查系统时间,类似于您现在的代码。此代码应在上午9点或之后每天发送一次电子邮件。你的计时器间隔越小,越精确到上午9点。例如,如果您将计时器间隔保持在60分钟,服务将每小时检查一次系统时间,电子邮件将在上午9点到10点之间发送。如果您将计时器设置为10分钟,服务将每分钟检查一次系统时间,并在上午9:00到9:10之间发送电子邮件

此方法不会随着时间的推移而失去同步,因为它使用系统时钟,而不是计时器间隔来知道何时触发

删除lastRun DateTime字段及其所有引用。删除标记字段和引用。添加名为nextRun的DateTime字段:

private DateTime nextRun = DateTime.MinValue;
添加函数GetNextRun:

private static DateTime GetNextRun(DateTime lastRun)
{
    var next = lastRun.AddDays(1);
    return new DateTime(next.Year, next.Month, next.Day, 9, 0, 0); 
}
将ScheduleTimer运行时间更改为:

protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    if (DateTime.Now < nextRun) return;

    nextRun = GetNextRun(DateTime.Now);
    ServiceEmailMethod();
}
protectedvoid scheduleTimer_已过(对象发送方,ElapsedEventArgs e)
{
if(DateTime.Now
如果你关心墙上的时间,那么计时器是不适合这项工作的工具。你需要一个调度程序。而且,如果您的服务的唯一目的是等到预定的时间,然后运行有用的代码,那么最好创建一个预定的任务,而不是让您的代码(毫无意义地)在所有剩余的时间运行。@Damien_不信者,谢谢,有用的评论。