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)
If BCMode = 0 Then
My.Computer.Audio.Play(System.AppDomain.CurrentDomain.BaseDirectory & "\buzz.wav", AudioPlayMode.Background)
ElseIf BCMode = 1 Then
txtBarcode.Text = Barcode
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)
If P = Len(PartBarcode) Then
PartBarcode = ""
PartBarcode = Mid(PartBarcode, P + 1, Len(PartBarcode) - P)