Error: Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
After doing some research online I found when I add Control.CheckForIllegalCrossThreadCalls = False it works but I feel like that is not the proper way to handle this error. If anyone has a better way of handling this by starting a new thread or something please show me how.
Visual Studio 2008 Dev Edition
Imports sds = System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports System.Data.SqlClient
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim WithEvents mySerialPort As New SerialPort("COM1")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False 'when removed I recieve error
mySerialPort.BaudRate = 9600
mySerialPort.Parity = Parity.None
mySerialPort.StopBits = StopBits.One
mySerialPort.DataBits = 8
mySerialPort.Handshake = Handshake.None
AddHandler mySerialPort.DataReceived, AddressOf DataReceviedHandler
mySerialPort.Open()
End Sub
Private Sub DataReceviedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles mySerialPort.DataReceived
mySerialPort = CType(sender, SerialPort)
Dim int As Integer = mySerialPort.BytesToRead
Dim indata As String = mySerialPort.ReadExisting()
MsgBox(indata)
TextBox1.Text = indata
End Sub