首页 > 解决方案 > 检查互联网连接计时器

问题描述

我想要一个代码,当用户禁用互联网连接时,关闭 chrome.exe

正如你在下面看到的。

   Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim ip = "www.google.com"
    Dim timeout = 1000
    Dim sw = New Stopwatch()
    myProcesses = Process.GetProcessesByName("chrome")
    Try
        Dim ping As Long = -1
        sw.Start()
        If My.Computer.Network.Ping(ip, timeout) Then
            sw.Stop()
            ping = sw.ElapsedMilliseconds
            For Each myProcess In myProcesses
                If myProcess IsNot Nothing Then
                    myProcess.Kill()
                    MessageBox.Show("Internet down")
                    Me.Close()
                End If
            Next
        End If
        If ping < 500 Then

        Else

        End If
    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub

你能帮我解决这个问题吗?我还启用了 Timer1,所以我认为它应该可以正常工作。

感谢大家的帮助,对于此类小问题,很抱歉耽误您的时间。

标签: vb.nettimerbasic

解决方案


你可以像我一样尝试一些简单的方法:

Class Form1
    Private Const IP As String = "216.58.196.68" ' Google's IP
    Private Const Timeout = 3000
    Dim i As Integer

    ' To trigger the Clock
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Clock.Interval = Timeout ' 3 sec
        Clock.Start() ' Let the timer name be "Clock"
    End Sub

    Private Sub Clock_Tick(sender As Object, e As EventArgs) Handles Clock.Tick
        SendPacket(IP)
    End Sub

    Private Sub SendPacket(IP As String)
        If My.Computer.Network.IsAvailable Then
            Try
                If My.Computer.Network.Ping(IP) Then ' Default timeout = 1500
                Else
                    MsgBox("Network is not working.")
                End If

            Catch ex As Exception
                MsgBox("Network is unreachable.")
                Dim MR As MsgBoxResult = MsgBox("Retry?", MsgBoxStyle.YesNo, "Retry?")

                If MR = MsgBoxResult.Yes Then
                    SendPacket(IP)
                Else
                    End ' Exits the app
                End If
            End Try
        Else
            MsgBox("Internet is down.")

            ProcKiller()
            Clock.Stop()
        End If
    End Sub

    Private Sub ProcKiller()
        i = UBound(Process.GetProcessesByName("Chrome"))

        For x As Integer = i To 0 Step -1
            Process.GetProcessesByName("Chrome")(x).Kill()
        Next

        MsgBox("Killed Successfully")
    End Sub
End Class

注意:但我从未使用此方法杀死任何应用程序。


推荐阅读