Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am developing an application for some kind of hardware in which i want to receive data from hardware device through serial port and store this data in a variable and display it in textbox.
but when i try to display in text box then it gives an error of Thread Cross detection.
"Cross-thread operation not valid: Control TextBox1 accessed from a thread other than the thread it was created on"
Imports System.Data.OleDb
Imports System.IO.Ports

Public Class Student_Entry
    Dim str12 As String
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim da As OleDbDataAdapter
    Dim str As String
    Dim icount As Integer
    Dim rcount As Integer
    Dim data As DataTable
    Dim response As String, deviceadd As String
    Dim c As Integer, i As Integer
    Dim datareceive As String
    Dim deviceNum As Integer, devicetemp As Integer
 Private Sub Student_Entry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            For Each sp As String In SerialPort.GetPortNames
                portcode = sp.ToString()
            Next
            With SerialPort1
                .PortName = portcode
                .BaudRate = 115200
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.Two
                .Open()
            End With
        Catch ex As Exception
            MsgBox("Please Change Port setting")
        End Try
    End Sub
'Receiving data from serial port when device active
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        On Error Resume Next
        With SerialPort1
            response = ""
            datareceive = SerialPort1.ReadExisting()
            For i As Integer = 1 To Len(datareceive)
                c = Asc(Mid(datareceive, i, 2))
                response = response & Hex(c) & " "
            Next
            If Mid(response, 1, 2) = "7B" And Mid(response, 31, 2) = "7D" And Len(response) = 33 Then
                deviceadd = Mid(response, 14, 11)
                MsgBox(deviceadd)   'This msg box working properly 
                TextBox1.Text = deviceadd  'But the value of deviceadd is not display in TextBox1 it generates cross thread detection problem
                deviceNum = Mid(response, 23, 1)
                devicetemp = Mid(response, 1, 23)
                'ans = Mid(response, 25, 1)
               
            End If
        End With
    End Sub



Please Give me proper solution for this problem
Thanks buddy...
Posted

Ok Ok Finally I had got solution for this problem and i would like to share it with my friends.....
I have added this function working clearly....
First I check there is any cross thread condition occur if yes then i fix it

Imports System.Data.OleDb
Imports System.IO.Ports
 
Public Class Student_Entry
    Dim str12 As String
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim da As OleDbDataAdapter
    Dim str As String
    Dim icount As Integer
    Dim rcount As Integer
    Dim data As DataTable
    Dim response As String, deviceadd As String
    Dim c As Integer, i As Integer
    Dim datareceive As String
    Dim deviceNum As Integer, devicetemp As Integer
 Private Sub Student_Entry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            For Each sp As String In SerialPort.GetPortNames
                portcode = sp.ToString()
            Next
            With SerialPort1
                .PortName = portcode
                .BaudRate = 115200
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.Two
                .Open()
            End With
        Catch ex As Exception
            MsgBox("Please Change Port setting")
        End Try
    End Sub
'Receiving data from serial port when device active
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        On Error Resume Next
        With SerialPort1
            response = ""
            datareceive = SerialPort1.ReadExisting()
            For i As Integer = 1 To Len(datareceive)
                c = Asc(Mid(datareceive, i, 2))
                response = response & Hex(c) & " "
            Next
            If Mid(response, 1, 2) = "7B" And Mid(response, 31, 2) = "7D" And Len(response) = 33 Then
                deviceadd = Mid(response, 14, 11)
                MsgBox(deviceadd)   'This msg box working properly
                'Instead of below step we calling a method 
               ' TextBox1.Text = deviceadd  
                call chekthread()               
            End If
        End With
    End Sub

'Check Thread cross 
Private Sub chekthread()
        Try
         If (TextBox1.InvokeRequired) Then
               
                TextBox1.Invoke(New MethodInvoker(AddressOf chekthread), New Object() {Text})
                
            End If
            TextBox1.Text = deviceadd.ToString()

Thanks to my self
 
Share this answer
 
Comments
Member 10726183 6-Dec-16 7:23am    
Thanks to you.. \m/
Or in 4.0 you can call the textbox like this:

VB
'Receiving data from serial port when device active
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        On Error Resume Next
        With SerialPort1
            response = ""
            datareceive = SerialPort1.ReadExisting()
            For i As Integer = 1 To Len(datareceive)
                c = Asc(Mid(datareceive, i, 2))
                response = response & Hex(c) & " "
            Next
            If Mid(response, 1, 2) = "7B" And Mid(response, 31, 2) = "7D" And Len(response) = 33 Then
                deviceadd = Mid(response, 14, 11)
                MsgBox(deviceadd)   
        
               Me.Invoke(Sub()
                   TextBox1.Text = deviceadd
                         End Sub)
            End If
        End With
    End Sub
 
Share this answer
 
You cannot directly access a UI control from threads other than the one that created it. To do so, you have to use the IsInvokeRequired InvokeRequired property and the Invoke() method. Google these terms to find out more.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 15:10pm    
My 4. Sometimes BeginInvoke is better, not IsInvokeRequired but InvokeRequired; not really needed if always called from non-UI thread; needed only for methods called from different threads.
--SA
[no name] 18-Jul-11 15:15pm    
yes, little confused. I'm an idealist and I thought the property name must start with an 'Is' to convey the idea that it is a boolean property. Anyway, thanks for correcting me :-)
[no name] 18-Jul-11 15:19pm    
Answer corrected.
Hi
Please go through with these links

Cross Thread

Another Solution


If you search your problem on google then you will find so many solution with code.

Regards
AR
 
Share this answer
 
what is portcode ?

I got an error on the code :
VB
For Each sp As String In SerialPort.GetPortNames
                portcode = sp.ToString()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900