Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:57:34+00:00 2026-05-28T07:57:34+00:00

maybe I am being stooped… but the fact is that I am a bit

  • 0

maybe I am being stooped… but the fact is that I am a bit of a n00b concerning threading…

I am making use of a serial port in a class. I am raising an event from that class to my form calling the class. Event contains data received…

I wish to simply populate a textbox from the raised event.

Now I am not specifically creating a seperate thread, but I get the normal crossthreading error when trying to update my textbox on the UI, so my assumption is that the serial port and its internal methods probably creates its own threads…

Regardless, I am a bit confused as to how to properly implement an invoke, from my main form, pointing to the thread in the instantiated class…

I hope this makes sense…


Dim WithEvents tmpRS232 As New clsRS232

Private Sub but_txt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_txt.Click
    tmpRS232.Set_com_port("COM8", 38400)
    tmpRS232.Transmit_data(txt_tx.Text)
End Sub

Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned
    txt_rx.Text = str_data 'Cross threading error
    MsgBox(str_data) 'Fires without errors
End Sub

Can someone please provide a code example based on this code?

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:57:35+00:00Added an answer on May 28, 2026 at 7:57 am

    You are correct, the issue here is that you are attempting to update a UI element from a non-UI thread (in this case the serial port handler). What you need to do is check if the InvokeRequired flag is set on the control that you are trying to access from the callback. If so that means that you need to marshall your call to the UI thread. You can achieve this by using either Invoke or BeginInvoke from System.Windows.Forms.Control.

    Private Delegate Sub SetRxTextCallback(ByVal [text] As String)
    
    Private Sub SetRxText(ByVal [text] As String)
        txt_rx.Text = [text]
    End Sub
    
    Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned
    
        If (txt_rx.InvokeRequired) Then
            Dim d As New SetRxTextCallback(AddressOf Me.SetRxText)
            Me.BeginInvoke(d, New Object() {[str_data]})
        End If
    
        'txt_rx.Text = str_data 'Cross threading error
        'MsgBox(str_data) 'Fires without errors
    End Sub
    

    Here’s a link to the MSDN documentation that explains it in detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I am being stupid, but this isn't making sense to me... I have
Maybe I am being a bit paranoid, but as I am re-writing a contact
Maybe it's wrong but I always use this query for my app: cme_only =
I just thought about this maybe being useful, but I can't find a way
Maybe I'm being picky, but in javadoc there is a following information about LinkedHashSet
Maybe I'm being stupid but I can't figure out why a pointer is being
Maybe I'm being misled by my profiler (Netbeans), but I'm seeing some odd behavior,
Maybe I am being daft, or misunderstanding either WAMP restrictions or browser restrictions, but
Maybe the title is a little missleading but I'd like to ask you for
Maybe my title is not the best but here is what I really want.I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.