code_junkie

How can I raise an event every hour (or specific time interval each hour) in .NET?

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. 00:20
  2. 00:40
  3. 01:00
  4. 01:20
  5. 01:40
  6.  
  7. int CalculateTimerInterval(int minute)
  8. {
  9. if (minute <= 0)
  10. minute = 60;
  11. DateTime now = DateTime.Now;
  12.  
  13. DateTime future = now.AddMinutes((minute - (now.Minute % minute))).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
  14.  
  15. TimeSpan interval = future - now;
  16.  
  17. return (int)interval.TotalMilliseconds;
  18. }
  19.  
  20. static System.Windows.Forms.Timer t;
  21. const int CHECK_INTERVAL = 20;
  22.  
  23.  
  24. static void Main()
  25. {
  26. t = new System.Windows.Forms.Timer();
  27. t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
  28. t.Tick += new EventHandler(t_Tick);
  29. t.Start();
  30. }
  31.  
  32. static void t_Tick(object sender, EventArgs e)
  33. {
  34. t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
  35. }
  36.  
  37. int minutes = DateTime.Now.Minute;
  38. int adjust = 10 - (minutes % 10);
  39. timer.Interval = adjust * 60;
  40.  
  41. public Form1()
  42. {
  43. InitializeComponent();
  44.  
  45. //some fake data, obviously you would have your own.
  46. DateTime someStart = DateTime.Now.AddMinutes(1);
  47. TimeSpan someInterval = TimeSpan.FromMinutes(2);
  48.  
  49. //sample call
  50. StartTimer(someStart,someInterval,doSomething);
  51. }
  52.  
  53. //just a fake function to call
  54. private bool doSomething()
  55. {
  56. DialogResult keepGoing = MessageBox.Show("Hey, I did something! Keep Going?","Something!",MessageBoxButtons.YesNo);
  57. return (keepGoing == DialogResult.Yes);
  58. }
  59.  
  60. //The following is the actual guts.. and can be transplanted to an actual class.
  61. private delegate void voidFunc<P1,P2,P3>(P1 p1,P2 p2,P3 p3);
  62. public void StartTimer(DateTime startTime, TimeSpan interval, Func<bool> action)
  63. {
  64. voidFunc<DateTime,TimeSpan,Func<bool>> Timer = TimedThread;
  65. Timer.BeginInvoke(startTime,interval,action,null,null);
  66. }
  67.  
  68. private void TimedThread(DateTime startTime, TimeSpan interval, Func<bool> action)
  69. {
  70. bool keepRunning = true;
  71. DateTime NextExecute = startTime;
  72. while(keepRunning)
  73. {
  74. if (DateTime.Now > NextExecute)
  75. {
  76. keepRunning = action.Invoke();
  77. NextExecute = NextExecute.Add(interval);
  78. }
  79. //could parameterize resolution.
  80. Thread.Sleep(1000);
  81. }
  82. }
  83.  
  84. private Timer _timer;
  85. private Int32 _hours = 0;
  86. private Int32 _runAt = 3;
  87.  
  88. protected override void OnStart(string[] args)
  89. {
  90. _hours = (24 - (DateTime.Now.Hour + 1)) + _runAt;
  91. _timer = new Timer();
  92. _timer.Interval = _hours * 60 * 60 * 1000;
  93. _timer.Elapsed += new ElapsedEventHandler(Tick);
  94. _timer.Start();
  95. }
  96.  
  97. void Tick(object sender, ElapsedEventArgs e)
  98. {
  99. if (_hours != 24)
  100. {
  101. _hours = 24;
  102. _timer.Interval = _hours * 60 * 60 * 1000;
  103. }
  104.  
  105. RunImport();
  106. }
Add Comment
Please, Sign In to add comment