I have a windows form app as follows. I would like to make conditional transactions according to current time.Lets say if time is 11 and minute is between 10 and 15 then do this else do other thing on run time. How can I do it?
Best Regards.
Here is my sample code:
C#
[STAThread] static void Main() { DateTime td = DateTime.Now; int hour = td.Hour; int min = td.Minute; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (hour >= 11 && hour 12 && min >= 10 && min 15) { Application.Run(new Form1(hour, min)); } else { Application.Run(new Form1()); } }
VulpesRank #55 · 29.2K reputationPosted Jan 4, 2012, 2:11 AM EST
However, you could do this:
Notice that the accuracy of the System.Windows.Forms.Timer leaves something to be desired but, if you set it to fire at 11.05, it should certainly remain within the desired interval even if the program is running continuously for a number of days.
If the program is running 'for ever', it might be a good idea to recalibrate it each time it fires.
VulpesRank #55 · 29.2K reputationPosted Jan 3, 2012, 11:55 PM EST
All you need to do is to add an extra constructor to Form1 which takes two int parameters: hour and minute, save them to fields on the form and you're there.
RaysefoRank #4,173 · 360 reputationPosted Jan 4, 2012, 1:03 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 4, 2012, 1:14 AM EST
RaysefoRank #4,173 · 360 reputationPosted Jan 4, 2012, 1:35 AM EST
Thanks for your help.I wonder is there a way to set this Timer for a specific time interval? I mean everyday between 23:00 and 23:10.
Best Regards.
RaysefoRank #4,173 · 360 reputationPosted Jan 4, 2012, 2:48 AM EST
It does NOT helping me to trigger the tick event after the application is running.
Here is my main:
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }public Form1() { InitializeComponent(); this.ShowInTaskbar = false; this.Refresh(); Hook.CreateHook(KeyReaderr); writeUp = ""; } private void Form1_Load(object sender, EventArgs e) { DateTime td = DateTime.Now; int hour = td.Hour; int min = td.Minute; if (hour >= 14 && hour 15 && min >= 43 && min 44) { timer1.Interval = 1; // fires almost immediately } else if (hour 14) { DateTime dtElevenToday = DateTime.Today.AddHours(11).AddMinutes(5); // fires at 11.05 today timer1.Interval = (int)(dtElevenToday - td).TotalMilliseconds; } else { DateTime dtElevenTomorrow = DateTime.Today.AddHours(35).AddMinutes(5); // fires at 11.05 tomorrow timer1.Interval = (int)(dtElevenTomorrow - td).TotalMilliseconds; } timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Interval = (int)TimeSpan.FromHours(24).TotalMilliseconds; // next fires in 24 hours // rest of code Hook.DestroyHook(); writeUp = ""; CreateZipFile(@"S:\logs\", "LOG_" + name + ".zip", @"S:\logs\log_" + name + ".txt"); File.Delete(@"S:\logs\log_" + name + ".txt"); }VulpesRank #55 · 29.2K reputationPosted Jan 4, 2012, 4:47 AM EST
RaysefoRank #4,173 · 360 reputationPosted Jan 4, 2012, 9:09 PM EST
as soon as I enabled the timer , It worked !
Thanks.