Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

C# show timer in textbox

I'm trying to make a timer run in a textbox and I haven't had any luck.

This is the code I'm using:

private static System.Timers.Timer timer;
...
private void StartBtn_Click(object sender, EventArgs e)
{
    timer = new System.Timers.Timer(1000);
    timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    timer.Enabled = true;
}
...
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    TimeTb.Text = e.SignalTime.ToString();
}

But nothing happens.

I tried this:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    MessageBox.Show(e.SignalTime.ToString(), 
    "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}

And it worked fine. Anyone know why it isn't working with my textbox?

Upvotes: 1

Views: 10202

Answers (6)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

you could do something like

private void Button_Click(object sender, RoutedEventArgs e)
    {
        this._timer = new DispatcherTimer();
        this._timer.Interval = TimeSpan.FromMilliseconds(1);
        this._timer.Tick += new EventHandler(_timer_Tick);
        _timer.Start();
    }

void _timer_Tick(object sender, EventArgs e)
{
    t= t.Add(TimeSpan.FromMilliseconds(1));
    textBox.Text = t.Hours + ":" + t.Minutes + ":" + t.Seconds + ":" + t.Milliseconds;
}

Edit :

How to add Assembly: WindowsBase

enter image description here

Upvotes: 0

Ralf
Ralf

Reputation: 1352

The Elapsed Event runs on a different Thread then the UI. Its not allowed to manipulate UI Objects from a different thread and an Exception should surface in your EventHandler. Since you don't handle Exceptions there you won't notice it.

In StartBtn_Click set the SynchronizingObject Property of the Timer to this (the form). Then the elapsed Event will be synchronized with the main thread.

Upvotes: 4

Justin Harvey
Justin Harvey

Reputation: 14672

Your OnTimedEvent callback will not be called on the UI thread, so you will get an exception in there trying to set the textbox text. So you need to change the event handler to look like this:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
  if (TimeTb.InvokeRequired)
  {
    TimeTb.Invoke((MethodInvoker)delegate
                {
                  OnTimedEvent(source, e);
                });

  }

  TimeTb.Text = e.SignalTime.ToString();
}

Upvotes: 1

Ric
Ric

Reputation: 13248

Put a try catch around OnTimedEvent and see if there are any issues with threading. If there are, try using the System.Windows.Forms.Timer which can resolve the issues of cross threading.

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

As stated:

Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

Upvotes: 1

Anderson Pimentel
Anderson Pimentel

Reputation: 5767

Maybe using Application.DoEvents(); after TimeTb.Text = e.SignalTime.ToString(); would work, though using it would not be recommended (Use of Application.DoEvents()).

Upvotes: 0

Stofkn
Stofkn

Reputation: 1488

May be you should start the timer first.

Take a look at:

http://msdn.microsoft.com/en-us/library/system.timers.timer.start.aspx

So add:

timer.Start();

Upvotes: 1

Related Questions