Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 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?
Posted

1 solution

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
 
Share this answer
 
Comments
vrushali katkade 1-Nov-11 2:16am    
but i want to test this code how to incresed the total elapsed milisecond of the code
Sergey Alexandrovich Kryukov 1-Nov-11 11:34am    
Yes, you want to test it, no "ifs", no "buts".
So, what's the problem? Just learn the capability of Task Scheduler. It can handle much more complex schedules than you do. For example, something like "every working day of first week of month at certain time" and the like. It can do what you want as well, just try it.
--SA
Espen Harlinn 1-Nov-11 9:02am    
Good advice, and a nice set of links :)
Sergey Alexandrovich Kryukov 1-Nov-11 11:33am    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900