Nate Bross Posted July 10, 2005 Posted July 10, 2005 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. 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? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Joe Mamma Posted July 10, 2005 Posted July 10, 2005 (edited) 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: 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: 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 Edited July 10, 2005 by Joe Mamma Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Nate Bross Posted July 14, 2005 Author Posted July 14, 2005 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Joe Mamma Posted July 14, 2005 Posted July 14, 2005 (edited) 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. . . 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: 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. . . 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) Edited July 14, 2005 by Joe Mamma Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Nate Bross Posted September 30, 2005 Author Posted September 30, 2005 I completely forgot about this thread, sorry. The above sovled my problem. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.