• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Invoke form objects


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?

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
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:

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now