错误未被捕获(AsyncCallback)

时间:2012-10-15 02:47:31

标签: vb.net sockets asynchronous error-handling

您好我有一段使用套接字连接到服务器的代码

Try
    'Connect To The Server
    Dim socketclient As New TcpClient
    Dim success
    Dim result
    result = socketclient.BeginConnect(ip, port, socket.EndConnect, socketclient)
    success = result.AsyncWaitHandle.WaitOne(5000, True)
    If socketclient.Connected = False Then
        Throw New Exception("Server is offline")
    End If
catch ex as exception

End Try

如你所见。如果服务器脱机,我的try-catch设置不会捕获错误“主机未知”我相信这是因为在异步调用期间发生错误。如果这是真的,那么如何在异步调用中捕获错误?

1 个答案:

答案 0 :(得分:0)

您应该结束异步操作。 你的等待是完全没必要的,如果你只想阻止等待连接只需通过套接字调用EndConnect;) http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.endconnect.aspx