How to customize timer to fire my routine at specific time
-
i am using this code
using System;
using System.Windows.Forms;
using System.Timers;namespace T_TEST
{
public partial class Form1 : Form
{
static System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { schedule\_Timer(); } static void schedule\_Timer() { Console.WriteLine("### Timer Started ###"); DateTime nowTime = DateTime.Now; DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\] if (nowTime > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds; timer = new System.Timers.Timer(tickTime); timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed); timer.Start(); } static void timer\_Elapsed(object sender, ElapsedEventArgs e) { //Console.WriteLine("### Timer Stopped ### \\n"); timer.Stop(); MessageBox.Show("Hello World!!!"); //Console.WriteLine("### Scheduled Task Started ### \\n\\n"); //Console.WriteLine("Hello World!!! - Performing scheduled task\\n"); //Console.WriteLine("### Task Finished ### \\n\\n"); schedule\_Timer(); } }
}
the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks
tbhattacharjee
-
i am using this code
using System;
using System.Windows.Forms;
using System.Timers;namespace T_TEST
{
public partial class Form1 : Form
{
static System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { schedule\_Timer(); } static void schedule\_Timer() { Console.WriteLine("### Timer Started ###"); DateTime nowTime = DateTime.Now; DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\] if (nowTime > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds; timer = new System.Timers.Timer(tickTime); timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed); timer.Start(); } static void timer\_Elapsed(object sender, ElapsedEventArgs e) { //Console.WriteLine("### Timer Stopped ### \\n"); timer.Stop(); MessageBox.Show("Hello World!!!"); //Console.WriteLine("### Scheduled Task Started ### \\n\\n"); //Console.WriteLine("Hello World!!! - Performing scheduled task\\n"); //Console.WriteLine("### Task Finished ### \\n\\n"); schedule\_Timer(); } }
}
the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks
tbhattacharjee
-
i am using this code
using System;
using System.Windows.Forms;
using System.Timers;namespace T_TEST
{
public partial class Form1 : Form
{
static System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
}private void Form1\_Load(object sender, EventArgs e) { schedule\_Timer(); } static void schedule\_Timer() { Console.WriteLine("### Timer Started ###"); DateTime nowTime = DateTime.Now; DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\] if (nowTime > scheduledTime) { scheduledTime = scheduledTime.AddDays(1); } double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds; timer = new System.Timers.Timer(tickTime); timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed); timer.Start(); } static void timer\_Elapsed(object sender, ElapsedEventArgs e) { //Console.WriteLine("### Timer Stopped ### \\n"); timer.Stop(); MessageBox.Show("Hello World!!!"); //Console.WriteLine("### Scheduled Task Started ### \\n\\n"); //Console.WriteLine("Hello World!!! - Performing scheduled task\\n"); //Console.WriteLine("### Task Finished ### \\n\\n"); schedule\_Timer(); } }
}
the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks
tbhattacharjee
Because you set the tick time to a specific value, means that it fires at that interval, not that it fires at a specific time: it doesn't add the tick interval to the system clock when you start the timer, it sets a specific number of ticks that should elapse before the timer event is fired. Changing the system clock does not affect that tick count, so if you set a timer for one hour and then move the clock forward 59 minutes, it will still be an hour before the timer fires. If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Because you set the tick time to a specific value, means that it fires at that interval, not that it fires at a specific time: it doesn't add the tick interval to the system clock when you start the timer, it sets a specific number of ticks that should elapse before the timer event is fired. Changing the system clock does not affect that tick count, so if you set a timer for one hour and then move the clock forward 59 minutes, it will still be an hour before the timer fires. If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks
tbhattacharjee
-
can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks
tbhattacharjee
You should be able to do that yourself, if you wrote the code you show! You know how to set the interval on the timer, and you know how to create variables...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks
tbhattacharjee
- call your timer (for example) every Minute - set timer.tick to 60000 - when you have the timer.event don't stop the timer - compare if now is greater or equal your schedule-time, but less your schedule-time +30 seconds - if yes the give the message and increase your schedule-time