System.Timers.Timer timer = new System.Timers.Timer();
private void button1_Click(object sender, EventArgs e)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 2000;
timer.Enabled = true;
timer.Start();
}
private void timer_Elapsed(object myobject, System.Timers.ElapsedEventArgs e)
{
Say("Time hit" + DateTime.Now.ToString());
}
what am i missing?
EDIT:
Tried to add:
timer.AutoReset = true;
For those curious Say Method is:
private void Say(string s)
{
try
{
txtSay.AppendText(s + "\r\n");
}
catch
{
}
}
The say method doesn't have a problem in there. Works with everything else.
I think you didint mentioned that you getting cross thread exception.. try to change code like that:
Invoke(new Action(()=>Say("Time hit" + DateTime.Now.ToString())));
Because
And then you swallow that exception without any trace
private void Say(string s)
{
try
{
txtSay.AppendText(s + "\r\n");
}
catch // empty catch-block: the real fundamental problem
{
}
}
So the Timer event does fire but it produces an error that remains hidden.
The short, practical solution would be to use a Windows.Timer for a GUI related timer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With