vb.net - 检查互联网连接计时器
问题描述
我想要一个代码,当用户禁用互联网连接时,关闭 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,所以我认为它应该可以正常工作。
感谢大家的帮助,对于此类小问题,很抱歉耽误您的时间。
解决方案
你可以像我一样尝试一些简单的方法:
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
注意:但我从未使用此方法杀死任何应用程序。
推荐阅读
- git - 尝试切换分支时出现“权限被拒绝”错误
- windows - 詹金斯奴隶无法连接到詹金斯大师
- r - 当我使用 openxlsx 将其转换为 excel 时,如何在 R 中保留我的数据框的日期格式?
- java - 正则表达式模式检测两个连续数字后跟最多 9 个字符?
- python - 被阻止时如何在Scrapy中暂停蜘蛛
- reactjs - babel 7 不允许在装饰器和类之间使用 export 关键字。请改用`export @dec class`
- networking - 计算网络地址
- python - 如何从向导 Odoo 11 编写另一个字段
- database - Laravel 和 Websockets 中的实时数据库
- angular - 在 Build 目录中找不到 Angular 6 延迟加载模块以及另外 2 个应用程序