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
896 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

11 Experts available now in Live!

Get 1:1 Help Now