R
RFleming
I have a class that spawns a thread in a sub class to monitor data on
a socket connection. Once data is found I read it as bytes and want
to send it back to the parent class to a PopulateStatusRecord
subroutine. I tried make the subroutine shared, but could not use the
me keyword, so I don’t know how to pass the variable returndata
outside of the thread and back to the parent class. The code below is
psudeo code in areas to keep the readabilty down. Any help would be
greatly appreciated!
Thanks
Ryan
Public Module Test
Sub Main
Dim clsMyClass As MyClass = New MyClass
clsMyClass.StartCommunications
End Sub
End Module
Public Class MyClass
Private thdMain As Threading.Thread
Private WithEvents clsMainThread As MainThread
Public Sub PopulateStatusRecord(ByVal BytesReceived() As Byte)
With Me.Status
Value1 = BytesReceived(6)
Value2 = BytesReceived(7 to 10)
End With
End Sub
Public Sub StartCommunications()
If clsMainThread Is Nothing Then
clsMainThread = New MainThread
clsMainThread.IPAddress = “192.168.1.5”
clsMainThread.Port = “5555”
End If
If thdMain Is Nothing Then thdMain = New
Threading.Thread(AddressOf clsMainThread.MainThread)
If thdMain Is Nothing Then thdMain = New
Threading.Thread(AddressOf clsMainThread.MainThread)
clsMainThread.blnStopThread = False
thdMain.IsBackground = True
thdMain.Start()
End If
End Sub
Private Class MainThread
Public Event DataReceived(ByVal Length As Int16, ByVal
ByteBuffer() As Byte)
Public blnStopThread As Boolean = False
Private SckCon As New System.Net.Sockets.TcpClient
Public IPAddress as string
Public Port as integer
Public Sub MainThread()
Debug.Print("Starting Thread " +
Threading.Thread.CurrentThread.Name)
While blnStopThread = False
Threading.Thread.Sleep(5)
If Me.OpenSocket = True Then
If SckCon.GetStream.CanRead = True Then
Do Until DataAvailable = False
(read in data as bytes)
Loop
RaiseEvent DataReceived(intBytesRead,
BytesRead)
End If
End If
End While
Debug.Print(Threading.Thread.CurrentThread.Name + " thread
is exiting....")
End Sub
Protected Function OpenSocket() As Boolean
(If socket closed open it, if successful returns true)
End Function
Private Sub MainThread_DataReceived(ByVal Length As Short,
ByVal ByteBuffer() As Byte) Handles Me.DataReceived
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I can’t figure out how to pass bytebuffer outside of this
thread and back to the parent class
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
PopulateStatusRecord(ByteBuffer)
End Sub
End Class
End Class
a socket connection. Once data is found I read it as bytes and want
to send it back to the parent class to a PopulateStatusRecord
subroutine. I tried make the subroutine shared, but could not use the
me keyword, so I don’t know how to pass the variable returndata
outside of the thread and back to the parent class. The code below is
psudeo code in areas to keep the readabilty down. Any help would be
greatly appreciated!
Thanks
Ryan
Public Module Test
Sub Main
Dim clsMyClass As MyClass = New MyClass
clsMyClass.StartCommunications
End Sub
End Module
Public Class MyClass
Private thdMain As Threading.Thread
Private WithEvents clsMainThread As MainThread
Public Sub PopulateStatusRecord(ByVal BytesReceived() As Byte)
With Me.Status
Value1 = BytesReceived(6)
Value2 = BytesReceived(7 to 10)
End With
End Sub
Public Sub StartCommunications()
If clsMainThread Is Nothing Then
clsMainThread = New MainThread
clsMainThread.IPAddress = “192.168.1.5”
clsMainThread.Port = “5555”
End If
If thdMain Is Nothing Then thdMain = New
Threading.Thread(AddressOf clsMainThread.MainThread)
If thdMain Is Nothing Then thdMain = New
Threading.Thread(AddressOf clsMainThread.MainThread)
clsMainThread.blnStopThread = False
thdMain.IsBackground = True
thdMain.Start()
End If
End Sub
Private Class MainThread
Public Event DataReceived(ByVal Length As Int16, ByVal
ByteBuffer() As Byte)
Public blnStopThread As Boolean = False
Private SckCon As New System.Net.Sockets.TcpClient
Public IPAddress as string
Public Port as integer
Public Sub MainThread()
Debug.Print("Starting Thread " +
Threading.Thread.CurrentThread.Name)
While blnStopThread = False
Threading.Thread.Sleep(5)
If Me.OpenSocket = True Then
If SckCon.GetStream.CanRead = True Then
Do Until DataAvailable = False
(read in data as bytes)
Loop
RaiseEvent DataReceived(intBytesRead,
BytesRead)
End If
End If
End While
Debug.Print(Threading.Thread.CurrentThread.Name + " thread
is exiting....")
End Sub
Protected Function OpenSocket() As Boolean
(If socket closed open it, if successful returns true)
End Function
Private Sub MainThread_DataReceived(ByVal Length As Short,
ByVal ByteBuffer() As Byte) Handles Me.DataReceived
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I can’t figure out how to pass bytebuffer outside of this
thread and back to the parent class
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
PopulateStatusRecord(ByteBuffer)
End Sub
End Class
End Class