0

I want to have a code that when the users internet connection disabled, shut down the chrome.exe

as you can see below this.

   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

Can you help me with this, where is the problem with it ? Also I enabled the Timer1 so I think it should work properly.

Thanks for help to everyone, sorry to take your time for this type of small problems.

braX
  • 11,506
  • 5
  • 20
  • 33
  • `Ping()` method returns true or false depending on if the address can be pinged. It appears you need a `Not` in front of it. – jwatts1980 Jan 23 '20 at 05:36
  • you mean like this?; `If My.Computer.Network.Ping(ip, Not timeout) Then` – Sammy06 Jan 23 '20 at 05:38
  • Hello Aybe, Unfortunately there is no explanation for visual basic in that link and im a new developer, that's why I can't figure it out to find it :( :( im sorry – Sammy06 Jan 23 '20 at 06:31

1 Answers1

0

You may try some simple method as I do:

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

Note: But I never used this method to kill any application.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Firstly, thanks for answering but this code doesn't work unfortunately. :( anyway, thanks im still looking for an answer. – Sammy06 Jan 23 '20 at 06:27
  • Just I've seen the Process killing method doesn't performs its action. Rest network checking is working well. – Rohan Bari Jan 23 '20 at 06:35
  • Hello, thanks I'm working on that, rest of codes work properly, thanks again. – Sammy06 Jan 23 '20 at 06:49
  • @Sammy06 I've re-worked on the code, and this updated one is now working successfully. Whenever the internet disconnects, it'll automatically kill the Chrome.exe and its child processes. You should retry now. – Rohan Bari Jan 24 '20 at 07:59
  • Thanks a lot @LinuX Man it was very helpful, im sorry for late reply. – Sammy06 Jan 30 '20 at 17:06