Guys I know this question has been asked many times but I still cannot find a single reply that makes sense to me.
I have a form with over 400 controls on it.
I have a background thread that polls a bunch of equipment and gathers all kinds of data to be displayed on the form.
I then call one method "UpdateDisplay(string[] data)". This one routine takes all the information in the string array data[] and fills in all the components on the form. I have Labels, TextBoxes being filled in. Panels and TableLayouts and other controls being shown and hidden.
HUNDREDS of them!!
If I have to test each and every component to see if I have to invoke my program will turn into 5 billion lines of code!!!!!
Is there some way to simply see if the entire UpdateDisplay method needs to be invoked on the UI thread rather then all 400+ components it touches?
I put the following code:
if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(() => UpdateDisplay(data)));
}
as the first statement in the display method and I no longer get the run time exception about calling ui components from a non-ui thread.
followed by the rest of the update method with hundreds of components being updated with the information in data[]
But now I am getting a bunch of System.InvalidOperationException in System.Forms.dll???
If I set the debug exception option to break on all invalidoperationexceptions I see that they are being thrown randomly when updating components within the UpdateDisplay method with the nastygram about updating components from the non-ui thread.
Can someone please help me understand and fix this?
I can post the entire UpdateDisplay method to show how rediculous it would be if I had to wrap every component update call with an invokerequired if statement. Not exaggerating it would add like three lines of code per control or roughly 1200 lines of additional code!! That is just crazy!