I am writing a program to send SMS every 5 minutes. I want to stop this when the system time is 6 PM. How can I do that? This is my current code. I want to modify this accordin
You can use the below condition to check system TIME.
`if(DateTime.Now.Hour.ToString("HH") == "18") //Hour is in 24hour format`
Generally never use Sleep is bad idea , use timer instead:
System.Timers.Timer SendSMS = new System.Timers.Timer();
SendSMS.Interval = 300000; ///300000ms=5min
SendSMS.Elapsed += new System.Timers.ElapsedEventHandler(SendSMS_Elapsed);
SendSMS.Enabled=true;
void SendSMS_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
sms.SendSMS();
}
Add a second timer to check the time and stop when is 6pm:
System.Timers.Timer StopSendSMS = new System.Timers.Timer();
StopSendSMS.Interval = 100;
StopSendSMS.Elapsed += new System.Timers.ElapsedEventHandler( StopSendSMS_Elapsed);
StopSendSMS.Enabled=true;
void StopSendSMS_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (int(DateTime.Now.Hour)==18))
{
SendSMS.Enabled=false;
StopSendSMS.Enabled=false; ///no need to check anymore
}
}