R
randywheeler
Guest
Public Class Form1
Private Sub tcpTimer_Tick(sender As Object, e As System.EventArgs) Handles tcPTimer.Tick
Try
If tcpListen.Pending Then
If tbLog.TextLength > 50000 Then tbLog.Clear()
Dim proc As New Processing()
Dim tcpThread As System.Threading.Thread = New System.Threading.Thread(AddressOf proc.incoming)
tcpThread.SetApartmentState(System.Threading.ApartmentState.STA)
tcpThread.Start()
End If
Catch ex As Exception
tbLog.AppendText(Format(Now, "yyyy-MM-dd HH:mm:ss") & " - Error starting TCP thread. " & ex.Message & vbCrLf & vbCrLf)
End Try
End Sub
Delegate Sub SetTextCallback(myText As String)
Public Shared Sub SetText(myText As String)
If Form1.tbLog.InvokeRequired Then
Form1.tbLog.Invoke(New SetTextCallback(AddressOf SetText))
Else
Form1.tbLog.AppendText(myText)
End If
End Sub
End Class
Public Class Processing
Public Shared Sub GetIncoming(bytes() As Byte, stream As System.IO.Stream, flag As String)
Dim dataSN As Integer
Dim firstEvent As Integer
Dim lastEvent As Integer
dataSN = 256 * bytes(2) + bytes(3)
If dataSN = 0 Then
Form1.SetText(Format(Now, "yyyy-MM-dd HH:mm:ss") & " - report received, but the reported serial number is zero." & vbCrLf)
Exit Sub
End If
End Sub
End Class
I understand using invoke to update a UI control from a thread running in the same class. But what if the attempt to update the control is in another class? In the code, Form1.SetText in the Processing Class does not update the text box tbLog in Form1. I assume this has something to do with referencing from the thread Form1.SetText. I cannot figure out how to properly update tbLog in Form1 from the Processing Class.
Continue reading...
Private Sub tcpTimer_Tick(sender As Object, e As System.EventArgs) Handles tcPTimer.Tick
Try
If tcpListen.Pending Then
If tbLog.TextLength > 50000 Then tbLog.Clear()
Dim proc As New Processing()
Dim tcpThread As System.Threading.Thread = New System.Threading.Thread(AddressOf proc.incoming)
tcpThread.SetApartmentState(System.Threading.ApartmentState.STA)
tcpThread.Start()
End If
Catch ex As Exception
tbLog.AppendText(Format(Now, "yyyy-MM-dd HH:mm:ss") & " - Error starting TCP thread. " & ex.Message & vbCrLf & vbCrLf)
End Try
End Sub
Delegate Sub SetTextCallback(myText As String)
Public Shared Sub SetText(myText As String)
If Form1.tbLog.InvokeRequired Then
Form1.tbLog.Invoke(New SetTextCallback(AddressOf SetText))
Else
Form1.tbLog.AppendText(myText)
End If
End Sub
End Class
Public Class Processing
Public Shared Sub GetIncoming(bytes() As Byte, stream As System.IO.Stream, flag As String)
Dim dataSN As Integer
Dim firstEvent As Integer
Dim lastEvent As Integer
dataSN = 256 * bytes(2) + bytes(3)
If dataSN = 0 Then
Form1.SetText(Format(Now, "yyyy-MM-dd HH:mm:ss") & " - report received, but the reported serial number is zero." & vbCrLf)
Exit Sub
End If
End Sub
End Class
I understand using invoke to update a UI control from a thread running in the same class. But what if the attempt to update the control is in another class? In the code, Form1.SetText in the Processing Class does not update the text box tbLog in Form1. I assume this has something to do with referencing from the thread Form1.SetText. I cannot figure out how to properly update tbLog in Form1 from the Processing Class.
Continue reading...