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,146
  • 7
  • 35
  • 63
ReignOfComputer
  • 747
  • 2
  • 10
  • 28

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,102
  • 13
  • 25