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
911 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
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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