在VB.Net中,我正在尝试编写一个应用程序,该应用程序将使用此代码检查应用程序是否正在运行
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim success
Dim result
result = s.BeginConnect("127.0.0.1", 8485, New AsyncCallback(AddressOf s.EndConnect), s)
success = result.AsyncWaitHandle.WaitOne(1000, True)
'Label12.Text = "Online"
'Label12.ForeColor = Color.LimeGreen
If success = result.AsyncWaitHandle.WaitOne(1000, True) Then
Label12.Text = "Online"
Label12.ForeColor = Color.LimeGreen
End If
Dim s2 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim success2
Dim result2
result2 = s2.BeginConnect("127.0.0.1", 8484, New AsyncCallback(AddressOf s2.EndConnect), s2)
success2 = result2.AsyncWaitHandle.WaitOne(1000, True)
'Label27.Text = "Online"
'Label27.ForeColor = Color.LimeGreen
If success2 = result.AsyncWaitHandle.WaitOne(1000, True) Then
Label27.Text = "Online"
Label27.ForeColor = Color.LimeGreen
End If
Dim s3 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim success3
Dim result3
result3 = s3.BeginConnect("127.0.0.1", 1111, New AsyncCallback(AddressOf s3.EndConnect), s3)
success3 = result3.AsyncWaitHandle.WaitOne(1000, True)
'Label46.Text = "Online"
'Label46.ForeColor = Color.LimeGreen
If success3 = result.AsyncWaitHandle.WaitOne(1000, True) Then
Label46.Text = "Online"
Label46.ForeColor = Color.LimeGreen
End If
如果所有这些应用程序都处于联机状态,则应用程序可以正常运行,但是如果其中一个处于脱机状态,则Visual Studio会抛出“SocketException未处理”
有关如何解决这个问题的建议吗?