返回首页

我想excute system.threading.timer每天在固定的时间。我用下面的代码

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.   

    }  

} 
我想测试此代码,但是当我excute首次定时器,它将在2011年1月11日10:00:00 excute然后我不得不更改日期和时间2011年2月11日09:59:00,但它会于2011年2月11日10:00:00如何测试此代码不执行?| vrushali katkade:SAKryukov

回答