Invoke form objects

Posted on 2006-05-26
Last Modified: 2010-04-23

I have recently upgraded a project from .NET 1.1 to .NET 2.0 and have also removed Sax.Communications and built a Serial Class based on IO.Ports.SerialPort

But now because the DataReceived event is operating in a different thread it sends a shockwave through my application causing every form control to need something similar to this

Private Delegate Sub myDelegate(ByVal c As Control, ByVal message As String)

    Public Sub InvokeFormObject(ByVal c As Control, ByVal message As String)
        c.Invoke(New myDelegate(AddressOf UpdateControl), New Object() {c, message})
    End Sub

    Private Sub UpdateControl(ByVal c As Control, ByVal message As String)
        c.Text = message
    End Sub

And thats just to update a text box on screen.

I have hundreds of controls that will need this.

Is there any other way to do this?

Question by:adam_pedley
    1 Comment
    LVL 85

    Accepted Solution

    That's basically how it's done...   =)

    This is not the correct way of doing it, but if you only have one thread updating the GUI then you can turn OFF the cross-thread exception feature and take out all the Delegate/Invoke shenanigans by adding this line to your forms load event:

        Control.CheckForIllegalCrossThreadCalls = False

    Here are the docs:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now