HOME > .NET Framework > Forum > vb.net error : cross-thread operation not valid control 'rtbRecieved' accessed from a thread other than the thread it was created on
vb.net error : cross-thread operation not valid control 'rtbRecieved' accessed from a thread other than the thread it was created on
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
DBconnection()
End Sub
Private Sub DBconnection()
Dim myConnection As MySqlConnection
myConnection = New MySqlConnection("Server=161.246.195.45; User Id = nantawat;Password = 123456; Database = kmitl_database;")
myConnection.Open()
Dim intNumRows As Integer
Dim myCommand As MySqlCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT COUNT(*) FROM dt_in_out WHERE card_id = '" & rtbReceived.Text & "' AND dt_out = '' "
intNumRows = myCommand.ExecuteScalar()
If intNumRows > 0 Then
myCommand.CommandText = "UPDATE dt_in_out SET dt_out = '" & lbTime.Text & "' WHERE card_id = '" & rtbReceived.Text & "' AND dt_out = ''"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
Else
myCommand.CommandText = "INSERT INTO dt_in_out(card_id, dt_in)" & _
" VALUES('" & rtbReceived.Text & "','" & lbTime.Text & "')"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
End If
Me.Hide()
Dim f As New Show_Time
f.Show()
End Sub
ผมต้องการที่จะรับค่าจาก COM Port แล้วนำไปเก็บไว้ในฐานข้อมูล แต่มัน error รับค่ามาได้แล้ว แต่พอจะเอาไปเก็บในฐานข้อมูล มันก็ขึ้นว่า " cross-thread operation not valid : control 'rtbRecieved' accessed from a thread other than the thread it was created on. " โดยที่ rtbRecieved คือ Richtextbox ที่แสดงข้อความที่รับมาจาก com port ผมต้องแก้โค๊ดหรือเขียนเพิ่มตรงไหนบ้างครับ ขอบคุณครับ