hello everyone,
I am using System.Timers.Timer class. its object is myTimer and am using myTimer_Elapsed event to raise the event. I have used two masked textboxes that takes start and stop time as input and one button. The timer is instantiated in button click event . The code is shown below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO.Ports;
using System.Timers;

namespace LoginPage
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
       // private System.Timers.Timer myTimer;
           

            string start, stop;
            string start1, stop1;
           
            


                

        private void button1_Click(object sender, EventArgs e)
        {
             start = maskedTextBox1.Text;
             stop = maskedTextBox2.Text;
             DateTime start1 = DateTime.ParseExact(start, "HH:mm", info);
             DateTime stop1 = DateTime.ParseExact(stop, "HH:mm", info);
             DateTime min1 = DateTime.ParseExact(min, "HH:mm", info);
             DateTime max1 = DateTime.ParseExact(max, "HH:mm", info);
             if ((start1 >= stop1))
            {
              
                  if(stop1 >= min1 && stop1 <= max1)
                  {
                      MessageBox.Show("Invalid Entry!", "Warning");
                   }                                }
                
                   else
                   {
                       stop1 = stop1.AddDays(1);
        
                   }
               
            }
                        
            
             System.Timers.Timer myTimer = new System.Timers.Timer();
             myTimer.Enabled = true;
             myTimer.Interval = 1000;
             myTimer.AutoReset = false;
             myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
            // GC.KeepAlive(myTimer); 
             
         }

       

            
         public void Form3_Load(object sender, EventArgs e)
        {
            // Other initialization code
            maskedTextBox1.Mask = "00:00";
            maskedTextBox2.Mask = "00:00";            

           
        }

       
         void myTimer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
        {
            string i = Convert.ToString(e.SignalTime);
            do
            {
                         
                     if(Convert.ToString(start)== Convert.ToString(e.SignalTime))
                         break;
                         
                   else
                   MessageBox.Show("SignalTime: " + Convert.ToString(e.SignalTime)   + "\n" + "Start: " + start);

                         
                     //    i = i + 1;
 
                }while(i == Convert.ToString(start));
   
              do
              {
                         
                     if(Convert.ToString(stop)== Convert.ToString(e.SignalTime))
                         break;
                         
                   else
                   MessageBox.Show("SignalTime: " + Convert.ToString(e.SignalTime)   + "\n" + "Stop: " + stop);

                         
                     //    i = i + 1;
 
                }while(i == Convert.ToString(stop));

             }
                   
           }
}

The problem is in myTimer_Elapsed event and the do while loop. I want that the message box should display message when signal Time i.e. system time is equal to start time and in another do while loop when signal time is equal to stop time.

But the coding that I have done display message box even when the signal time is not equal to start and stop time respectively.
Please see if any one can help...
thanx in advance.

From your code:

if(Convert.ToString(start)== Convert.ToString(e.SignalTime))

start and stop are both strings, stop converting them to strings. You are just wasting time.

You can also stop using Convert.ToString(). Every object has a ToString() method so all you are doing is adding more complexity to something that is easy to do e.SignalTime.ToString() instead of Convert.ToString(e.SignalTime) . There is an added benefit you'll see later.

Given that, start and stop both have the format of "hh:mm" but e.SignalTime, when converted to a string, has the format "MM/d/yyyy h:mm:ss tt" so the two will never be equal.

How do we solve this issue? Well, one thing that using the ToString() method gives you is the ability to specify the format of the output. So all we need to do add the format specifier:

if (start == SignalTime.ToString("HH:mm")) {

(We use "HH" instead of "hh" to specify we want the time in 24 hour mode (0-23) rather than 12 hour mode (1-12)).

myTimer.Enabled = true;
             myTimer.Interval = 1000;
             myTimer.AutoReset = true;
             myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
            // GC.KeepAlive(myTimer);

hi @Momernath thankx for your help... I tried to use:
if (start == SignalTime.ToString("HH:mm")) as you said but still there is some problem.

myTimer.Enabled = true;
myTimer.Interval = 1000;
myTimer.AutoReset = true;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
// GC.KeepAlive(myTimer); 

As I have declared the above statements in buttonclick event and used if-else condition as shown below:
string i = e.SignalTime.ToString("HH:mm");
if (i == start.ToString())
            {
                myTimer.Start();
                MessageBox.Show("SignalTime: " + e.SignalTime.ToString() + "\n" + "Start: " + start);
               
            }
            else 
            {
                myTimer.Stop();
                myTimer.Enabled = false;
            }

Now the problem is that though the signal time and start time get equal, the message box is not displayed on its own unless I click on button. If I click on button even after the signal time and start time get equal, the message box keeps on displaying.Ofcourse it then stops when condition gets false and contorl goes to else statement.

Can you please see whstz the problem and let me know...
Thanks......

The problem is most likely due to the compared times having a resolution of minutes and your timer tick executing every second.
This means that for a whole minute the comparison is true. [Edit] You might solve this by changing the string format to "HH:mm:ss" to include the seconds.

Try converting the times to DateTime types instead. This will allow you to compare the values directly (do not use strings in your comparison!!).

On a similar point why not use the DateTimePicker to get the start and stop times. These automatically convert the user entered values to a DateTime type for you.

Also, be aware that using the == comparator on DateTime when you are testing against the system time is extreamly unlikely to be true. Use > and < instead.

[Edit] I should also point out that if you use the DateTime type you should compare the TimeOfDay property to remove any possible confusion over what date value they contain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.