每天在固定时间执行system.threading.timer [英] excute the system.threading.timer daily at fixed timing

查看:633
本文介绍了每天在固定时间执行system.threading.timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在固定时间每天执行system.threading.timer。我使用了以下代码

I want to excute the system.threading.timer daily at fixed timing. I have used the following code

using System;  
using System.Threading;  
public static class Program  
{  
    static void Main(string[] args)  
    {  
        // get today's date at 10:00 AM  
        DateTime tenAM = DateTime.Today.AddHours(10);  
 
        // if 10:00 AM has passed, get tomorrow at 10:00 AM  
        if (DateTime.Now > tenAM)  
            tenAM = tenAM.AddDays(1);  
 
        // calculate milliseconds until the next 10:00 AM.  
        int timeToFirstExecution = (int)tenAM.Subtract(DateTime.Now).TotalMilliseconds;  
 
        // calculate the number of milliseconds in 24 hours.   
        int timeBetweenCalls =  (int)new TimeSpan(24, 0, 0).TotalMilliseconds;  
 
        // set the method to execute when the timer executes.   
        TimerCallback methodToExecute = ProcessFile;  
 
        // start the timer.  The timer will execute "ProcessFile" when the number of seconds between now and   
        // the next 10:00 AM elapse.  After that, it will execute every 24 hours.   
        System.Threading.Timer timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls );  
 
        // Block the main thread forever.  The timer will continue to execute.   
        Thread.Sleep(Timeout.Infinite);  
    }  
 
    public static void ProcessFile(object obj)  
    {  
        // do your processing here.   
    }  
} 



i想要测试这段代码,但是当我将在01-11-2011 10:00:00第一次执行计时器时它将执行然后我已更改日期&时间到02-11-2011 09:59:00但是它不会在02-11-2011 10:00:00执行如何测试这段代码?


i want to test this code but when i will excute the first time timer at 01-11-2011 10:00:00 it will excute then i have change the date & time to 02-11-2011 09:59:00 but it will not execute at 02-11-2011 10:00:00 how to test this code?

推荐答案

对于这样的事情,使用Windows服务而不是计时器会好得多。这种服务已经存在于Windows中,称为Task Sheduler。您可以使用它来安排您的事件,并让服务使用或多或少复杂的时序模式触发它们(这可能非常复杂)。



您可以使用该类 System.Threading.Tasks.TaskScheduler



请参阅:

http://en.wikipedia.org/wiki/Task_Scheduler [ ^ ],

http://msdn.microsoft.com/en-us/library/aa383614.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx [ ^ ],

http://social.msdn.microsoft.com/Forums/en-NZ/netfxnetcom/thread/bbf31e04-87c8-42aa-be6c-01ab4d2d9890 [ ^ ],



-SA
For such things it's much better to use Windows Service instead of timer. Such service already exists in Windows, called "Task Sheduler". You can use it to schedule your events and let the service trigger them using more or less complex timing schema (which can be really complex).

You can use the class System.Threading.Tasks.TaskScheduler.

See:
http://en.wikipedia.org/wiki/Task_Scheduler[^],
http://msdn.microsoft.com/en-us/library/aa383614.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx[^],
http://social.msdn.microsoft.com/Forums/en-NZ/netfxnetcom/thread/bbf31e04-87c8-42aa-be6c-01ab4d2d9890[^],

—SA


这篇关于每天在固定时间执行system.threading.timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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