Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C# .NET 2 Threading.Timer - time drifting
- public void MyTimerCallback(object something) {
- var now = DateTime.UtcNow;
- var shouldProbablyHaveRun = new DateTime(
- now.Year, now.Month, now.Day,
- now.Hour, now.Minute - (now.Minute % 10), 0);
- var nextRun = shouldProbablyHaveRun.AddMinutes(10.0);
- // Do stuff here!
- var diff = nextRun - DateTime.UtcNow;
- timer.Change(diff, new TimeSpan(-1));
- }
- if (DateTime.Now.Minute % 10) == 0
- while ((DateTime.Now - lastRunTime).TotalSeconds < 600)
- CurrentThread.Sleep(1000);
- public static void Main()
- {
- System.Timers.Timer aTimer = new System.Timers.Timer();
- aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
- // Set the Interval to 600 seconds.
- aTimer.Interval=600000;
- aTimer.Enabled=true;
- Console.WriteLine("Press 'q' to quit the sample.");
- while(Console.Read()!='q');
- }
- // Specify what you want to happen when the Elapsed event is raised.
- private static void OnTimedEvent(object source, ElapsedEventArgs e)
- {
- Console.WriteLine("10 minutes passed!");
- }
Add Comment
Please, Sign In to add comment