我将目标框架从3.5更改为4.0,没有其他更改。一个简单的ping与3.5一起工作,现在4.0的结果给出了Cancelled = True。任何想法可能会导致这种情况?NetworkInformation.Ping(),结果在.NET 4.0中取消,但不在3.5中
Public Sub StartInternetConnectionCheckEvery(ByVal seconds As Integer)
Dim timer As New Timer()
timer.Interval = seconds * 1000
AddHandler timer.Tick, AddressOf CheckIsConnectedToInternet
timer.Start()
End Sub
Private Sub CheckIsConnectedToInternetByTimer(ByVal sender As Object, ByVal e As System.EventArgs)
CheckIsConnectedToInternet()
End Sub
Public Sub CheckIsConnectedToInternet()
Using ping As New System.Net.NetworkInformation.Ping()
Dim address As String = "www.google.com"
ping.SendAsync(address, Nothing)
AddHandler ping.PingCompleted, AddressOf OnPingResult
End Using
End Sub
Private Sub OnPingResult(ByVal sender As Object, ByVal e As PingCompletedEventArgs)
If e.Cancelled Then
Trace.WriteLine("InternetHelper: Ping Cancelled")
Return
End If
IsConnectedToInternet = (e.Reply.Status = IPStatus.Success)
RaiseEvent InternetPingResult(IsConnectedToInternet)
End Sub
慢,但肯定的是,越来越多的大型网站不响应Ping。这不是你的代码中的错误,而是需要思考的问题。 – dbasnett