Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cross-thread operation not valid: Control 'txtBarcode' accessed from a thread other than the thread it was created on.

Posted on 2011-09-14
3
Medium Priority
?
931 Views
Last Modified: 2012-08-14
Hi,

I am new to VB.net, and I am getting highly confused by the following error:

Cross-thread operation not valid: Control 'txtBarcode' accessed from a thread other than the thread it was created on.

I understand the meaning of the error, and have read numerous articles on how to fix this, but frankly I can't follow the application of any of these articles to my code.

The project is pretty simple. The form has a number of fields, including one called txtBarcode, into which the numeric value of a scanned barcode should be shown.

A control called serPort (Syetem.IO.Ports.SerialPort) is used to receive the barcode scan, and this processes the barcode, and attempts to paste this code into the field, which is when the error occurs.

This is where I need a bit of handholding, rather than a link to another article that I cannot follow!

The code for receiving the barcode is as follows:

Private Sub ProcessBarcode(ByVal Barcode As String)

    Barcode = Replace(Replace(Barcode, "METROLOGIC", ""), Chr(13), "")

    If Len(Barcode) = 12 Then
      Barcode = AddBCCheck(Barcode)
    End If

    If BCMode = 0 Then
      My.Computer.Audio.Play(System.AppDomain.CurrentDomain.BaseDirectory & "\buzz.wav", AudioPlayMode.Background)
    ElseIf BCMode = 1 Then
      txtBarcode.Text = Barcode
    End If

  End Sub

  Private Sub serPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived

    Dim SerialString As String
    Dim P As Integer
    Dim BC As String

    SerialString = serPort.ReadExisting
    PartBarcode = Replace(PartBarcode & SerialString, Chr(10), "")

    Do While InStr(PartBarcode, Chr(13)) > 0
      P = InStr(PartBarcode, Chr(13))
      BC = Mid(PartBarcode, 1, P)
      ProcessBarcode(BC)
      If P = Len(PartBarcode) Then
        PartBarcode = ""
      Else
        PartBarcode = Mid(PartBarcode, P + 1, Len(PartBarcode) - P)
      End If
    Loop

  End Sub
0
Comment
Question by:rpm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 36538110
In ProcessBarcode()...

Change:

     txtBarcode.Text = text

To:

    UpdateTextBox(Barcode)

Using this new code:
Private Delegate Sub UpdateTextBoxDelegate(ByVal text As String)

    Private Sub UpdateTextBox(ByVal text As String)
        If txtBarcode.InvokeRequired Then
            txtBarcode.BeginInvoke(New UpdateTextBoxDelegate(AddressOf UpdateTextBox), New Object() { text })
        Else
            txtBarcode.Text = text
        End If
    End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36538123
where are these subs? Are they in another class to that of the form that contains the textbox?
0
 
LVL 1

Author Closing Comment

by:rpm
ID: 36538170
Excellent answer thank you ... just what I needed ... a simple answer to my problem, thank you very much!
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

721 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