• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

How do I access a classes objects from within a thread in a function in another class?

The following code shows me how to access a form's controls from within a thread in another class.  

My question is how can I modify the following code so I can access another classes objects (for example a collection) from within a thread in another class?

so Instead of frmServer you would have clsClients with a collection object in it.

--------------------------------------------------
Imports System.Text
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets


Public Class frmServer

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim tc As New clsUDPServer(Me, AddressOf UpdateLabel)
        Dim myThread As New Thread(AddressOf tc.Receive)

        myThread.Start()

    End Sub

    Private Sub UpdateLabel(ByVal newText As String)
        lblMessages.Text = newText
    End Sub
   
End Class

Public Class clsUDPServer

    Private _frm As frmServer
    Private objUDPServer As UdpClient
    Private objThread As Thread
    Private intPort As Integer = 6580

    Private _del As UpdateLabel
    Public Delegate Sub UpdateLabel(ByVal newText As String)


    Public Sub New(ByVal frm As frmServer, ByVal del As UpdateLabel)
        _frm = frm
        _del = del
    End Sub

    Private Sub Create()
        objUDPServer = New UdpClient(intPort)
    End Sub

    Private Sub Close()
        objUDPServer.Close()
    End Sub

    Public Sub Receive()

        Dim objIPEndPoint As New IPEndPoint(IPAddress.Any, intPort)
        Dim bytMessage As Byte()

        Create()

        _frm.Invoke(_del, "test")
        Close()
    End Sub
End Class
0
Hepen
Asked:
Hepen
  • 6
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Wel...

You're already passing in a reference to a Form in the Constructor:

    Public Sub New(ByVal frm As frmServer, ByVal del As UpdateLabel)

Why can't you pass in a reference to your Collection as well?
0
 
HepenAuthor Commented:
Because I want a class to hold the collection not the form. I want to keep the code organized and clean.


The following block leads to a cross thread error.  
*******************************************
    clsClients.AddToCollection(bytMessage)
*******************************************

I know how to use delegates and such to access a form's controls but now I don't know how to access another classes sub/functions while i'm in a seperate thread.

Public Class UDPServer

   Public Sub Create()
    Dim objThread as thread
    objThread = new thread(addressof Receive)
    objThread.Start
   End Sub

   Public Sub Receive()
    bytMessage = objUDPServer.Receive(objIPEndPoint)
    ******************************************* PROBLEM
    clsClients.AddToCollection(bytMessage)
    ******************************************* PROBLEM
   end sub

End Class

Public Class clsClients
  Private UsersCollection as Collection

  Public Sub AddToCollection(strText as string)
     userscollection.add(strtext)
     frmServer.listbox.datasource = userscollection
  End Sub

End Class
0
 
HepenAuthor Commented:
This is the last thing I swear man ... You are helping me so much :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HepenAuthor Commented:
Another reason is I want the form to be able to easily access any of the collections or global variables or functions/subs in clsClients.

clsUDPServer will simply get the data from the client, then pass that data to a bunch of objects/variables in clsClients then the form will access everything in clsClients.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How is clsClients getting its reference to frmServer?  You are getting the Cross Thread error because you are trying to update frmServer from inside the AddToCollection() sub.  So you would need to use a Delegate just as before.

I don't have a complete big picture yet...but it seems to me that the Event model I gave in your other question would still be useful here.  Make your UDPServer() raise an event with the bytMessage data and both the clsClients and frmServer classes can subscribe to it and use their own Delegates to handle the call if they need to.  This is what I meant by "decoupling" the UDPServer() class.  By making it raise an event, more than one class can receive it and do what they want with it.  You don't have to manage all the ins and outs of what another class should be invoking or not invoking...
0
 
HepenAuthor Commented:
Aaah i see....

Would you kill me if I asked for an example of that?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I can do that...but it will be awhile...   =\
0
 
HepenAuthor Commented:
Public Sub AddToCollection(strText as string)
   synclock me
     userscollection.add(strtext)
   end synclock
     frmServer.listbox.datasource = userscollection
  End Sub

Should that only be synclocked if more than one thread is trying to run that sub?

Thanks man. I'm trying to do it myself as well but you will most likely beat me to it.
0
 
HepenAuthor Commented:
Ok I did it. Can you just let me know on that synclock question. Thanks

Your answer was perfect.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry for the delay...

Glad you figured it out.  =)

You only need to SyncLock if more than one thread will be accessing the Collection at a time.

On a side note...the Collection in VB.Net is really just there for VB6 compatability.  The ArrayList is actually the preferred generic "collection object" in .Net (unless you're using VB.Net 2005, in which case it is the List).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now