我正在尝试让客户端尝试所有5秒钟连接到不需要联机的服务器。只有在线时才应该连接。好吧,如果服务器已经在线,然后客户端启动,则消息将毫无问题地发送。但如果客户端首先启动它会等待一段时间直到超时并停止尝试连接。所以我试图用命令获得一个循环:
Client = New TCPControl2(ip,64555)
我试着这样做:
Try
Client = New TCPControl2(ip, 64555)
Catch ex As Exception
MsgBox(ex.Message)
End Try
我可以在MsgBox中关于Timeout,但我不知道如何做一种尝试直到它连接或只是设置超时时间但我也不知道。
Private Client As TCPControl2
答案 0 :(得分:1)
我认为你想要实现的目标可以通过do while循环完成。您可以在此处阅读更多内容:https://msdn.microsoft.com/en-us/library/eked04a7.aspx
Dim isConnected As Boolean = false
Do
Try
Client = New TCPControl2(ip, 64555)
' Condition changing here.
if Client.IsConnected = true ' <-- example!
' it's connected
isConnected=true
end if
Catch ex As Exception
MsgBox(ex.Message)
End Try
Loop Until isConnected = true
答案 1 :(得分:0)
继续尝试,直到客户端连接到服务器? 如何使用while循环:
while(notConnected)
Try
Client = New TCPControl2(ip, 64555)
notConnected= connectedState!="success"
Catch ex As Exception
MsgBox(ex.Message)
End Try
end while
答案 2 :(得分:0)
以下代码将尝试连接指定的秒数,并在成功时返回TCPControl2
对象。
Function TryConnect(ByVal ip As String, ByVal port As Integer) As TCPControl2
While 1 = 1
Try
Dim client As New TCPControl2(ip, port)
Return client
Catch
End Try
Threading.Thread.Sleep(100)
End While
Return Nothing
End Function
<强>用法:强>
' -- try to connect.. wait indefinitely
Client = TryConnect(ip, 64555)
If Client Is Nothing Then
MsgBox("Unable to connect! Please check your internet connection.. blah blah.. whatever", MsgBoxStyle.Exclamation)
Else
' you connected successfully.. do whatever you want to do here..
'Client.WhaeverMethod()
End If
答案 3 :(得分:0)
此处找到C#答案的翻译(致@LBushkin):Cleanest way to write retry logic?
我重命名了一些东西,使它更加VB友好。如果所有重试都失败,我也将其更改为返回发现的第一个异常,如评论中所示:
Public Class Retry
Public Shared Sub Invoke(action As Action, retryInterval As TimeSpan, Optional retryCount As Integer = 3)
Invoke(Of Object)(Function()
action()
Return Nothing
End Function, retryInterval, retryCount)
End Sub
Public Shared Function Invoke(Of T)(action As Func(Of T), retryInterval As TimeSpan, Optional retryCount As Integer = 3) As T
Dim firstException As Exception = Nothing
For Retry = 0 To retryCount - 1
Try
Return action()
Catch ex As Exception
If firstException Is Nothing Then firstException = ex
Threading.Thread.Sleep(retryInterval)
End Try
Next
Throw firstException
End Function
End Class
用法:
Retry.Invoke(Sub() SomeSubThatCanFail(), TimeSpan.FromMilliseconds(25))
或
Dim i = Retry.Invoke(Function() SomeFunctionThatCanFail(), TimeSpan.FromMilliseconds(25))