Threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RFleming@nationalsteel.com

    Threading

    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 PopulateStatusR ecord
    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.Star tCommunications
    End Sub
    End Module

    Public Class MyClass
    Private thdMain As Threading.Threa d
    Private WithEvents clsMainThread As MainThread


    Public Sub PopulateStatusR ecord(ByVal BytesReceived() As Byte)
    With Me.Status
    Value1 = BytesReceived(6 )
    Value2 = BytesReceived(7 to 10)
    End With
    End Sub

    Public Sub StartCommunicat ions()
    If clsMainThread Is Nothing Then
    clsMainThread = New MainThread
    clsMainThread.I PAddress = “192.168.1.5”
    clsMainThread.P ort = “5555”
    End If

    If thdMain Is Nothing Then thdMain = New
    Threading.Threa d(AddressOf clsMainThread.M ainThread)
    If thdMain Is Nothing Then thdMain = New
    Threading.Threa d(AddressOf clsMainThread.M ainThread)
    clsMainThread.b lnStopThread = False
    thdMain.IsBackg round = True
    thdMain.Start()
    End If
    End Sub

    Private Class MainThread
    Public Event DataReceived(By Val Length As Int16, ByVal
    ByteBuffer() As Byte)
    Public blnStopThread As Boolean = False
    Private SckCon As New System.Net.Sock ets.TcpClient
    Public IPAddress as string
    Public Port as integer

    Public Sub MainThread()
    Debug.Print("St arting Thread " +
    Threading.Threa d.CurrentThread .Name)

    While blnStopThread = False
    Threading.Threa d.Sleep(5)

    If Me.OpenSocket = True Then
    If SckCon.GetStrea m.CanRead = True Then
    Do Until DataAvailable = False
    (read in data as bytes)
    Loop
    RaiseEvent DataReceived(in tBytesRead,
    BytesRead)
    End If
    End If
    End While

    Debug.Print(Thr eading.Thread.C urrentThread.Na me + " thread
    is exiting....")
    End Sub

    Protected Function OpenSocket() As Boolean
    (If socket closed open it, if successful returns true)
    End Function

    Private Sub MainThread_Data Received(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
    !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!
    PopulateStatusR ecord(ByteBuffe r)
    End Sub
    End Class
    End Class

  • cfps.Christian

    #2
    Re: Threading

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

    Private Delegate Sub DRDelegate(ByVa l Length as Short, ByVal
    ByteBuffer() as Byte)
    Private sub MainThread_Data Received(ByVal Length As Short, _
    ByVal ByteBuffer() As Byte) Handles Me.DataReceived
    If Me.InvokeRequir ed Then
    Me.Invoke(new DRDelegate(Addr essOf MainThread_Data Received))
    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.

    Comment

    Working...