Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C# Threading
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 31-Oct-11 20:55pm

1 solution

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

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 29 Jul 2013
Copyright © CodeProject, 1999-2014
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