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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 42
get row value in vb.net 4 18
VB.Net - Get ColumnNames of SQL Table 4 15
ASP.NET (VB) return a record 2 40
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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