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
HepenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.