Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (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 by Joe Mamma

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.

Posted
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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (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 by Joe Mamma

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.

  • 2 months later...
Posted
I completely forgot about this thread, sorry. The above sovled my problem.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...