Events in a Collection

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I have a class that has a "RecievedData" event, this event returns the data that was recieved from a TCP Client. I would like to make a Collection of these classes and handle the RecievedData event.

Visual Basic:
Public WithEvents cClient As clsClient
Public Clients As New Collection

Public Sub TCPListen()
                Randomize()
                Do Until bRunning = False
                    tcpListener.Start()
                    'Accept the pending client connection and return             
                    'a TcpClient initialized for communication. 
                    Dim tcpClient As TcpClient

                    Dim TheKeyID As String = Rnd(300).ToString
                    cClient = New clsClient(tcpListener.AcceptTcpClient(), TheKeyID)
                    Clients.Add(cClient, TheKeyID)
                Loop

                tcpListener.Stop()
            End Sub

What do I need to so I can handle events from the clsClient within the Collection?
 
I dont know why you need a collection. . .
Don't know why you need TheKeyID, wont GetHashCode() suffice?

I assume the event is a delegate prototyped as a simple eventhandler. . .

Add to the form a method such as this:

Code:
sub DataReceived(sender as object, e as EventArgs)
if not sender is clsClient return 
dim clnt as clsClient = DirectCast(sender, clsClient)
' do things with the client here 
end sub

change your code to:
Code:
Public Sub TCPListen()
  While bRunning <> False
	tcpListener.Start()
	'Accept the pending client connection and return 
	'a TcpClient initialized for communication. 
	With New clsClient(tcpListener.AcceptTcpClient())
	  AddHandler .RecievedData, AddressOf DataReceived
	End With 
  End While
  tcpListener.Stop()
End Sub
 
Last edited:
I guess I didn't phrase my question very well. What I want to do is achieve the same functionaliy 'vb 6.0 control arrays' provided. I have a dynamic number of clsClients; however, I want them all to fire the same code when they recieve data.
 
and that is what my code does. . . doesn't it???

God vb6 confused so many people. . . vb6 is the worst piece of cr*p ever released as a development tool. I have been dealing with some distribution issues this week that are a result of vb6 being such a dog. . . but I digress. . .

this snippet attaches DataReceived to the clsClient that is created. . .
Code:
With New clsClient(tcpListener.AcceptTcpClient())
	 AddHandler .RecievedData, AddressOf DataReceived
	End With

the fact that it has an event handler that has been assigned a delegate keeps it from being GC'd. . .

look at this. . .

its a Mock of your clsClient:
Code:
Public Class clsClient
	Public Event DataReceived As DataReceivedHandler
	Public Sub New()
		ImitateThreadedClient()
	End Sub
	Private Sub ProcessData()
		Dim args As New System.ComponentModel.CancelEventArgs
		Do
			RaiseEvent DataReceived(Me, args)
			System.Threading.Thread.Sleep(10000)
		Loop Until args.Cancel
	End Sub
	Private Sub ImitateThreadedClient()
		Dim thr As System.Threading.Thread = New System.Threading.Thread(AddressOf ProcessData)
		thr.Start()
	End Sub
End Class
 
Public Delegate Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

make an app, drop button 1 on the main form and put this code in the form. . .
Code:
	Public Sub HandleDataReceived(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
		MessageBox.Show(String.Format("{0} in handler - Hashcode: {1}", sender.ToString(), sender.GetHashCode()))
		e.Cancel = Me.DialogResult = DialogResult.Cancel
	End Sub
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		With New clsClient
			AddHandler .DataReceived, AddressOf HandleDataReceived
		End With
	End Sub
	Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
		Me.DialogResult = DialogResult.Cancel
	End Sub
run the app and watch the behavior (clicking the button imitates your AcceptTcpClient . . .click it a few times)
 
Last edited:
Back
Top