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.