Cross Thread Exception - Medium Only

The control can only be accessed by the thread that created it - I know that.

  • I have a DataGridView with a DataSource based on BindingList <>.
  • I have a workflow (not a GUI) that runs some fancy calculations / comparisons / etc. then adds / edits the object to / in the BindingList <>.
  • In the timer, the GUI thread is updated from BindingList <>.

This code works flawlessly - as long as I don't work in the environment. In the environment, when the .Add () method is called on the BindingList <>, I get this little error:

An Exception has occurred
EXCEPTION : Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
IN METHOD : get_Handle
AT LINE   : 0
CLASS     : System.Windows.Forms.Control

Please note that the name of the affected element is empty ... I would have thought that if the problem was with updating the BindingList <>, it would not matter if I worked in the environment or not. Despite everything, this is what I see. In addition .Add () succeeds even if an exception is thrown !!

Obviously, this is not very important in my working environment (yet?), Since this only happens in Studio; and yes, I could call the GUI thread to perform the add, or store the additional files in the place where the GUI thread will retrieve them later ... I'm not looking for work, but more so I am interested in the answer to this question:

Why does the error appear only in the studio?

+5
3

VS, MDA (Managed Debugging Assistant), . MDA , , , 100% , -, ( , 99% ).

Add.

: 100% ... ( , - Red Gate?!) , Control , , , , . , , , - , catch, ( , , , inst, BG - threadpool).

+4

VS, , , . . , , .

WinForms, , , add , , , . , , .

+3

, ; (, BindingSource.DataError). Visual Studio , , - .

, (DataGridView) , , . "" . , , . , , .

Depending on how you are doing the binding, you should be able to disable it and manually update it in a loop.

+1
source

All Articles