0

I took this code from a website since VS 2010 doesn't support the timeout for the TCP connections:

Private Function ConnectWithTimeout() As Boolean
    Dim ar As IAsyncResult = TCPClient.BeginConnect(IPAddress, TCPPort, Nothing, Nothing)
    Dim wh As System.Threading.WaitHandle = ar.AsyncWaitHandle

    Try
        If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2), False) Then
            TCPClient.Close()
            TCPClient = New System.Net.Sockets.TcpClient
            Throw New TimeoutException()
        End If
    Catch ex As Exception
        ThrowError("Timeout on connecting to " & IPAddress & " at port " & TCPPort & ".")
        Return False
    Finally
        wh.Close()
    End Try

    Return True
End Function

And it works fine, but everytime, it gives me this on the debug output:

"A first chance exception of type 'System.TimeoutException' occurred in"

Even if I'm catching all the exceptions. Is there a way to get rid of this exception message as it is handled?

I've tried this:

    Dim connectDone As New System.Threading.AutoResetEvent(False)
    TCPClient.BeginConnect(IPAddress, TCPPort, New AsyncCallback(Sub(ar As IAsyncResult)
                                                                     TCPClient.EndConnect(ar)
                                                                     connectDone.Set()
                                                                 End Sub), TCPClient)
    'client.BeginConnect("127.0.0.1", 80, new AsyncCallback(delegate( IAsyncResult ar ) { client.EndConnect( ar );  connectDone.Set(); }), client);

    If Not connectDone.WaitOne(2000) Then
        Debug.WriteLine("TIMEOUT")
        Return False
    End If

    Return True

But it gives me InvalidOperationException on the beginconnect line: BeginConnect cannot be called while another asynchronous operation is in progress on the same Socket.

Stefano
  • 131
  • 1
  • 2
  • 8
  • ** Is there a way to get rid of this exception message as it is handled?** -> No. For the meaning of "first chance" see for example: http://stackoverflow.com/questions/564681/what-is-a-first-chance-exception . Even if you catch an Exception, there obviously is/was one. That's what this message tells you. – igrimpe Dec 13 '12 at 14:03
  • Ok, thanks. So is there a workaround not to have the exception and have the same result? – Stefano Dec 13 '12 at 14:31
  • http://stackoverflow.com/questions/795574/c-sharp-how-do-i-stop-a-tcpclient-connect-process-when-im-ready-for-the-progr --> see last answer. No need to throw an exception anyway. – igrimpe Dec 13 '12 at 15:19
  • I'm converting the code in VB but doesn't seem to work fine. Don't know how to convert the delegate. – Stefano Dec 14 '12 at 11:38

1 Answers1

-1
Private Function ConnectWithTimeout() As Boolean
    Dim ar As IAsyncResult 
    Dim wh As System.Threading.WaitHandle  

    Try
          ar = TCPClient.BeginConnect(IPAddress, TCPPort, Nothing, Nothing)
          wh = ar.AsyncWaitHandle
    Cath ex as Exception
          'Code to catch exception
    End Try

    Try
        If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2), False) Then
            TCPClient.Close()
            TCPClient = New System.Net.Sockets.TcpClient
            Throw New TimeoutException()
    End If
    Catch ex As Exception
        ThrowError("Timeout on connecting to " & IPAddress & " at port " & TCPPort & ".")
        Return False
    Finally
        wh.Close()
    End Try

    Return True
End Function
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • 1
    Hello Guilermo, just posting code with no explanation isn't a good Stack Overflow answer. Instead, our goal is to create a resource of knowledge that helps folks for years to come. When there's no explanations, it may not be clear to the reader how this provides value. Consider an edit to improve this post. Good luck! :) – jamesmortensen Nov 03 '13 at 00:37