验证vb.net中的Internet连接 [英] Verify internet connection in vb.net

查看:59
本文介绍了验证vb.net中的Internet连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在VB.NET中写的:

I wrote this in 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,但我不知道如何使用它,我的应用程序崩溃了.

How to check internet connection every minute? I tried using a BackgroundWorker but I don't know how to use it and my application crashed.

推荐答案

替代方案之一是使用 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

这篇关于验证vb.net中的Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆