Threading Error

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I'm writing a windows forms application which receives events from an
external application, and then responds to those events by modifying things
on the form. It's capturing the event but on trying to modify the controls
on the form I'm getting the error:

[System.InvalidOperationException] = {"Cross-thread operation not valid:
Control 'Form1' accessed from a thread other than the thread it was created
on."}

Can anyone help ?
 
In general, a worker thread cannot do things to a form or its controls. MS
says that these functions have to be executed on the form's thread (aka the
UI thread). The general way you do this is with Invoke, BeginInvoke, and
EndInvoke. Read up on these functions in .net help.
 
JezB said:
I'm writing a windows forms application which receives events from an
external application, and then responds to those events by modifying things
on the form. It's capturing the event but on trying to modify the controls
on the form I'm getting the error:

[System.InvalidOperationException] = {"Cross-thread operation not valid:
Control 'Form1' accessed from a thread other than the thread it was created
on."}

Can anyone help ?

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
Back
Top