Thread exception on timer event
Posted on 2006-03-30
So from my main form, I instantiate a class that I wrote, that has a public DebugCode event. From my form, I call a function that starts a timer object, part of the class. When the timer elapsed event is called, it triggers my DebugCode event, which sends a value as a parameter. In my form's handler for this event, I try to put this value in a textbox. When I try to set the textbox text property, I get the following exception...
"Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on."
What the heck?? I looked at the associated help file and there is about three pages of sample code on the "right", "threadsafe" way to do this, and a property CheckForIllegalCrossThreadCalls that you can set to false. What's the deal with this, will I get in trouble if I suppress this check?? Are you really not supposed to do this very simple thing anymore? (If not, I might end up putting this on my "how they've broken VB with .NET" list.)