我將目標框架從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