Thread exception on timer event


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.)

Thanks.

riceman0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi riceman0,

They haven't broken anything...   =)

You just haven't experienced this type of thing before because VB6 was not multithreaded.  All languages in the .Net family deal with this issue and the correct way to handle is NOT to set the CheckForIllegalCrossThreadCalls  property to false.

The System.Timers.Timer and System.Threading.Timer runs in their own thread while the System.Windows.Forms.Timer does NOT run in its own thread.

To properly update a GUI control from a seperate thread you need to use a Delegate and the Invoke() method.

See here for a simple example of a Delegate:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21744615.html

Here is an example of running a thread in a Class and making that class raise events that are handled on the main UI:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21729609.html

Read those links and then ask any questions you might have...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
riceman0Author Commented:

Thanks.  

Esp. for the clarification that this is specific to non-form-based timers; that helps me not hold this against VB.NET.  :)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.