验证vb.net中的Internet连接

时间:2017-09-09 19:44:38

标签: vb.net backgroundworker internet-connection

我在VB.NET中写道:

Function Net() As Boolean
    Return My.Computer.Network.Ping("216.58.209.142")
End Function

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        If Net() = True Then
            InternetConnection.ForeColor = Color.Green
            InternetConnection.Text = "OK"
        ElseIf Net() = False Then
            InternetConnection.ForeColor = Color.White
            InternetConnection.BackColor = Color.Red
            InternetConnection.Text = "KO"
        End If
    Catch ex As Exception
        InternetConnection.Text = "KO"
    End Try
End Sub

如何每分钟检查互联网连接?我尝试使用BackgroundWorker但我不知道如何使用它并且我的应用程序崩溃了。

1 个答案:

答案 0 :(得分:0)

其中一种方法是使用Task.Delay

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CheckConnection()
End Sub

Async Sub CheckConnection()
    Await Task.Delay(60000)
    Try
        If Net() = True Then
            InternetConnection.ForeColor = Color.Green
            InternetConnection.Text = "OK"
        ElseIf Net() = False Then
            InternetConnection.ForeColor = Color.White
            InternetConnection.BackColor = Color.Red
            InternetConnection.Text = "KO"
        End If
    Catch ex As Exception
        InternetConnection.Text = "KO"
    End Try
    CheckConnection()
End Sub