Results 1 to 4 of 4

Thread: How to dispose an object with it's own thread?

  1. #1

    Thread Starter
    Junior Member Cakkie's Avatar
    Join Date
    May 2002
    Location
    Olen, Belgium
    Posts
    24

    How to dispose an object with it's own thread?

    I'm currently writing a multithreaded socket server, where each connection has it's own thread. Connections are represented by classes. Upon connection, a new instance in created, which starts a new thread for that connection, and accepts and processes the connection. The reference to the connection is stored in a collection, called colConnections which is maintained by a server class.
    So far so good. Now the connection is broken (either by client, server, or from the connection object) and I want to dispose the object. How do I go about? When it needs to be done from the server, it's no problem, but when the client breaks the connection or the connection object breaks the connection I need to get rid of the object.
    This might give a better view on the situation.
    Code:
    server
     -- connections
         -- connection
    So if the "server" closes the connection (like server shutting down), it just gets the object from the "connections" collection, and sets it to nothing.
    But when connection is broken by the connection object itself, I need to make sure it is gone completely.
    I was thinking of killing the thread that runs the connection object, but I'm not sure if that will do the trick. Will the object itself "disappear" when the thread is gone?

    Sorry for the difficult explanation
    Tom Cannaerts
    www.tom.be

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    I am having this exact same problem Anyone with some insight on this would be helpfull. I have buillt a multi-threaded server and I'm finding that when a client adbrupley disconnectect, the Socket.IsConnected flag does not change to false, which in turn keeps the socket alive. In VB6, with the socket control, it would fire an error and you could handle it there. I Fired up my server last night and connected to it through my web browser just to activate the connections.. Closed the web browser, and went to bed.. Woke up this morning to find there are still 2 live connections to my server and nothing connected to it. You would think the sockets would time themselves out after a period of time but I guess that is not the case. Anyone?

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Use events. Fire an event each time the connection is broken, then dispose of the object. Post some more code and I'll try to and figure something out when I get home.
    Dont gain the world and lose your soul

  4. #4
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Well it's not that easy, From what I have been reading as I scour the net for answers, the sockets we use in the Framework are stateless (A raw socket I assume). I have seen a couple of different ways to handle a Disconnect but they aren't that pretty. One way is to poll your sockets and send a packet periodically, it will throw an exeption if it's not connected, then you can handle the close on the execption. Surely there has got to be a cleaner, and better way to handle socket closing monitoring. I don't mind polling the sockets but there has got to be a way to check the state of the socket maybe (better than sending a dummy packet). I'll list the 3 MAIN subs that contribute to this problem but I don't think they will do much good for the cause.


    'Start the listner
    Public Sub StartServer()
    Dim aCnt As Int16

    'Make sure server isn't running
    If blnRunning Then Exit Sub

    'Set up the listener
    Listener = New TcpListener(ServPort) : Listener.Start()

    'Start the thread manager
    Dim X As New Thread(AddressOf ThreadMngr) : X.Start()

    'Start listening
    blnRunning = True

    RaiseEvent SetStatus("Server Started: " & Now)
    RaiseEvent ClientLogin()

    While blnRunning
    Try
    Thread.Sleep(100)
    Dim InSock As Socket = Listener.AcceptSocket
    For aCnt = 0 To Clients.Length - 1
    If Clients(aCnt) Is Nothing Then
    Clients(aCnt) = New clsClient(InSock, aCnt)
    AddHandler Clients(aCnt).GetMessage, AddressOf Clients(aCnt).HandleMessage
    Dim T As New Thread(AddressOf Clients(aCnt).Listen) : T.Start()
    RaiseEvent ClientLogin()
    Exit Try
    End If
    Next
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End While


    'The thread manager
    Private Sub ThreadMngr()
    Dim aCnt As Int16
    'Manage the client threading
    While Me.blnRunning
    Thread.Sleep(100)
    For aCnt = 0 To Clients.Length - 1
    If Not Clients(aCnt) Is Nothing Then
    If Not Clients(aCnt).ClientSocket.Connected Then
    Clients(aCnt) = Nothing
    RaiseEvent ClientLogout()
    End If
    End If
    Next
    End While
    End Sub







    'Class client code
    Public Class clsClient
    Private mSocket As Socket
    Private MyID As Int16
    Public Event GetMessage(ByVal strMsg As String)

    Public Sub New(ByVal tSock As Socket, ByVal Index As Int16)
    mSocket = tSock
    MyID = Index
    End Sub

    Public ReadOnly Property ClientID() As Int16
    'Return clients Index
    Get
    Return MyID
    End Get
    End Property

    Public ReadOnly Property ClientSocket() As Socket
    'Return clients Socket
    Get
    Return mSocket
    End Get
    End Property

    Public Sub Listen()
    Dim SB As System.Text.StringBuilder

    While Me.ClientSocket.Connected
    Thread.Sleep(100)
    Try
    If Me.ClientSocket.Available > 0 Then
    End If
    Catch ex As Exception
    MsgBox(ex.Message)
    Me.ClientSocket.Close()
    End Try

    End While

    End Sub

    'Where we get our data from
    Public Sub HandleMessage(ByVal strMsg As String)

    End Sub

    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width