Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have facing the big issue . I am not understand this is the code level issue or devices have some configuration issue. We have devices, need to get configuration details from devices through application. Below code are working fine for some deivces but some deivces are getting the below error after connecting the device. I am able to connect to device but it is not allowing me to the fire any command through application, manually device is working fine.I have asked to network guy they told me all devices have same configuration.

Please help me. :-)


Please anyone advise on how to resolve this issue. ask if you wish to see the code snippets


Error is : Cannot access a disposed object. object name 'system.net.sockets.socket' vb.net



VB
Private Sub ReceiveCallBack(ByVal ar As IAsyncResult)
        '--Retreive array of bytes

        Dim bytes() As Byte = (ar.AsyncState)
        Try
            '--Get number of bytes received and also clean up resources that was used from beginReceive
            Dim numBytes As Int32 = _mySocket.EndReceive(ar)
            '--Did we receive anything?
            If numBytes > 0 Then
                '--Resize the array to match the number of bytes received. Also keep the current data
                ReDim Preserve bytes(numBytes - 1)
                Dim received As String = _ascii.GetString(bytes)



                '--Now we need to raise the received event. 
                '  args() is used to pass an argument from this thread to the synchronized container's ui thread.
                Dim args(0) As Object

                '--Create a new delegate for the OnReceive event
                Dim d As New RaiseReceiveEvent(AddressOf OnReceive)

                If _lineMode = True Then
                    '--Yes, split the string into an array based on the EOL character
                    Dim sep() As Char = {EOLChar}
                    Dim lines() As String = received.Split(sep)
                    Dim i As Int32
                    '--Raise the received event once for every line of text!
                    For i = 0 To lines.Length - 1
                        If i = lines.Length - 1 Then
                            args(0) = lines(i)
                        Else
                            args(0) = lines(i) & EOLChar
                        End If
                        '--Invoke the private delegate from the thread. 
                        d.Invoke(args(0))
                    Next
                    i = Nothing

                Else
                    '--Not line mode. Pass the entire string at once with only one event
                    args(0) = received
                    '--Invoke the private delegate from the thread. 
                    d.Invoke(args(0))
                End If
                received = Nothing
            End If
        Catch ex As Exception
            '--Raise the exception event 
            Dim args() As Object = {ex}
            'Dim d As New RaiseExceptionEvent(AddressOf OnExcpetion)
            '_syncObject.Invoke(d, args)
            'd.Invoke(args(0))
            '--If not connected, raise the connected event
            If IsConnected() = False Then
                args(0) = False
                Dim dl As New RaiseConnectedEvent(AddressOf OnConnected)
                dl.Invoke(args(0))
                bytes = Nothing
                ar = Nothing
                '   _mySocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, bytes)
            End If
        End Try
        '--Are we stil conncted?
        If IsConnected() = False Then
            '--Raise the connect event
            Dim args() As Object = {False}
            Dim d As New RaiseConnectedEvent(AddressOf OnConnected)
            d.Invoke(False)
            bytes = Nothing
            ar = Nothing
            args = Nothing
        Else
            '--Yes, then resize bytes to packet size
            ReDim bytes(PacketSize - 1)
            '--Call BeginReceive again, catching any error
            Try

                _mySocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf ReceiveCallBack, bytes)



            Catch ex As Exception
                '--Raise the exception event 
                Dim args() As Object = {ex}
                Dim d As New RaiseExceptionEvent(AddressOf OnExcpetion)
                '_syncObject.Invoke(d, args)
                d.Invoke(args(0))
                '--If not connected, raise the connected event
                If IsConnected() = False Then
                    args(0) = False

                    ' Dim dl As New RaiseConnectedEvent(AddressOf OnConnected)
                    'dl.Invoke(args(0))
                    bytes = Nothing
                    ar = Nothing
                    args(0) = Nothing
                End If
            End Try
        End If

    End Sub
Posted
Updated 18-Mar-15 1:23am
v5
Comments
ZurdoDev 18-Mar-15 8:19am    
You'll need to debug the code. The error says you're trying to access something after it has been disposed.
lav naphade 19-Mar-15 5:13am    
Hi Ryan,
I have debuged the code, i have getting the time out expired after connecting to the device. please can you suggest how to resolve the this issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900