Thread exception on timer event

Posted on 2006-03-30
Last Modified: 2008-02-01

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


Question by:riceman0
    LVL 85

    Accepted Solution

    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:

    Here is an example of running a thread in a Class and making that class raise events that are handled on the main UI:

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

    Author Comment



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


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now