【问题标题】:Checking internet connection Timer [duplicate]检查互联网连接计时器[重复]
【发布时间】:2020-01-23 05:29:57
【问题描述】:

我想要一个代码,当用户的互联网连接被禁用时,关闭 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,所以我认为它应该可以正常工作。

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

【问题讨论】:

  • Ping() 方法返回真或假取决于地址是否可以被 ping 通。看来您需要在它前面加上Not
  • 你的意思是这样吗? If My.Computer.Network.Ping(ip, Not timeout) Then
  • 您好 Aybe,不幸的是,该链接中没有对 Visual Basic 的解释,我是一名新开发人员,这就是为什么我无法找到它的原因 :( :( 对不起

标签: vb.net timer basic


【解决方案1】:

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

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

注意:但我从来没有用这种方法杀死任何应用程序。

【讨论】:

  • 首先,感谢您的回答,但不幸的是,此代码不起作用。 :( 无论如何,谢谢我仍在寻找答案。
  • 刚才我看到进程终止方法没有执行它的操作。休息网络检查运行良好。
  • 您好,谢谢我正在处理,其余代码正常工作,再次感谢。
  • @Sammy06 我重新编写了代码,这个更新的代码现在可以成功运行。每当互联网断开连接时,它都会自动终止 Chrome.exe 及其子进程。您现在应该重试。
  • 非常感谢@LinuX Man 这对您很有帮助,抱歉回复晚了。
猜你喜欢
  • 2017-11-30
  • 2012-03-23
  • 1970-01-01
  • 2013-08-16
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多