0

I have the following code in my WinForms C# app:-

private static void displayTime(object source, ElapsedEventArgs e)
{
    timer++;
    timeTxtBox.Text = parseTime(timer);
}

This is throwing an InvalidOperationException with the message, with the details

Cross-thread operation not valid: Control 'timeBox' accessed from a thread other than the thread it was created on.

How would I make this work?

puretppc
  • 3,232
  • 8
  • 38
  • 65
ReignOfComputer
  • 747
  • 2
  • 10
  • 30

1 Answers1

1

Try this:

private static void displayTime(object source, ElapsedEventArgs e)
{ 
    timeTxtBox.Invoke(new Action(() => 
    {
    timer++;
    timeTxtBox.Text = parseTime(timer);
    }));
}
Behrad Farsi
  • 1,110
  • 13
  • 25