Click to See Complete Forum and Search --> : Accessing Forms controls from a timer callback


avatar.ds
July 17th, 2006, 02:49 PM
I need to update Forms controls based on processing done in a timer delegate.

This is what I get:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

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

Is there a workaround?

cilu
July 18th, 2006, 04:39 AM
Can you post the code you are using?

avatar.ds
July 19th, 2006, 10:27 AM
Cilu:

Thanks for your input, but it seems I found the solution. There is lots of code, so posting it would be problematic, but the problem was that I tried to access, say, "Label->Text=..." from a System::Timers::Timer->Elapsed callback which always runs on a separate thread.

The solution is to use the controls' Invoke method to launch a delegate that would access the control from the UI thread.