Threading

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
 
C

cfps.Christian

Looks like you're probably getting a thread access exception.

Private Delegate Sub DRDelegate(ByVal Length as Short, ByVal
ByteBuffer() as Byte)
Private sub MainThread_DataReceived(ByVal Length As Short, _
ByVal ByteBuffer() As Byte) Handles Me.DataReceived
If Me.InvokeRequired Then
Me.Invoke(new DRDelegate(AddressOf MainThread_DataReceived))
Else
'Do Work
End If
End Sub

If thats not what you're looking for then you might clarify what
you're attempting to do threadwise. From the looks of things you're
catching the event in the same class as you're raising it which is not
common.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top