Click here to Skip to main content
13,051,404 members (47,972 online)
Rate this:
Please Sign up or sign in to vote.
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.   
    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 31-Oct-11 19:55pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


sangita navale 1-Nov-11 2:16am
but i want to test this code how to incresed the total elapsed milisecond of the code
SAKryukov 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.
Espen Harlinn 1-Nov-11 9:02am
Good advice, and a nice set of links :)
SAKryukov 1-Nov-11 11:33am
Thank you, Espen.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 29 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100