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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

628 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