This is what I have:
Imports System.Threading
Public Class Form1
Public data2 As String
Dim yn As Integer
Public dataCapture As thread
Private Sub ThreadTask()
Dim sw As New Stopwatch
sw.Start()
Do
While sw.ElapsedMilliseconds <= 50
TextBox1.Text = SerialPort1.ReadLine
End While
sw.Reset()
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With SerialPort1
.PortName = "COM3"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
SerialPort1.Open()
MsgBox(" connected.")
dataCapture = New Thread(AddressOf ThreadTask)
dataCapture.IsBackground = True
dataCapture.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
yn = 0
End Sub
It works until the bolded line, involving TextBox1, when I get an error "Cross-thread call". I, for the life of me, can not figure out how to update the text in Textbox1. I have been looking at this for a week. Im sure there is a simple solution, I just cant understand it :)
Imports System.Threading
Public Class Form1
Public data2 As String
Dim yn As Integer
Public dataCapture As thread
Private Sub ThreadTask()
Dim sw As New Stopwatch
sw.Start()
Do
While sw.ElapsedMilliseconds <= 50
TextBox1.Text = SerialPort1.ReadLine
End While
sw.Reset()
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With SerialPort1
.PortName = "COM3"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
SerialPort1.Open()
MsgBox(" connected.")
dataCapture = New Thread(AddressOf ThreadTask)
dataCapture.IsBackground = True
dataCapture.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
yn = 0
End Sub
It works until the bolded line, involving TextBox1, when I get an error "Cross-thread call". I, for the life of me, can not figure out how to update the text in Textbox1. I have been looking at this for a week. Im sure there is a simple solution, I just cant understand it :)