none
How to display pc online or live at 3 second intervals RRS feed

  • Question

  • So I want to make an application that if the PC is online or on, the text on the button is green and if the PC is dead the text on the button is red using the interval timer

    I have made it using ping, but the results are lagging, I'm asking for a solution

    Private Sub tmrCek_Tick(sender As Object, e As EventArgs) Handles tmrCek.Tick
            If My.Computer.Network.Ping("192.168.0.101") Then
                Button1.ForeColor = Color.Green
            Else
                Button1.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.102") Then
                Button2.ForeColor = Color.Green
            Else
                Button2.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.103") Then
                Button3.ForeColor = Color.Green
            Else
                Button3.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.104") Then
                Button4.ForeColor = Color.Green
            Else
                Button4.ForeColor = Color.Red
            End If
        End Sub

    Sunday, February 16, 2020 9:27 AM

Answers

  • what about the tagging button?

    Perhaps a super simple example which I deviated from the first code sample.

    Imports System.Net.NetworkInformation
    
    Public Class Form1
        Private ButtonItemList As New List(Of Item)
        Private Async Function PinAddress(address As String, timeout As Integer) As Task(Of Boolean)
            Try
                Dim ping As New Ping()
                Dim reply = Await ping.SendPingAsync(address, timeout)
                Return (reply.Status = IPStatus.Success)
            Catch
                Return False
            End Try
        End Function
        Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            For Each item As Item In ButtonItemList
                If Await PinAddress(item.Address, 300) Then
                    item.Button.BackColor = Color.White
                Else
                    item.Button.BackColor = Color.Red
                End If
    
            Next
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ButtonItemList.Add(New Item() With {
                                  .Button = Button1,
                                  .Address = "docs.microsoft.com"})
            ButtonItemList.Add(New Item() With {
                                  .Button = Button2,
                                  .Address = "104.112.178.21"})
            ButtonItemList.Add(New Item() With {
                                  .Button = Button3,
                                  .Address = "2607:f8b0:400a:800::200e"})
        End Sub
    End Class
    Public Class Item
        Public Property Button() As Button
        Public Property Address() As String
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ahda Muammar Monday, February 17, 2020 3:54 AM
    Monday, February 17, 2020 3:42 AM
    Moderator

All replies

  • Lets see the existing code so that those who are willing to assist know what you have done so far and include the version of Visual Studio used and version of the .NET Framework e.g. VS2017, Framework 4.7.2

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, February 16, 2020 10:21 AM
    Moderator
  • Private Sub tmrCek_Tick(sender As Object, e As EventArgs) Handles tmrCek.Tick
            If My.Computer.Network.Ping("192.168.0.101") Then
                Button1.ForeColor = Color.Green
            Else
                Button1.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.102") Then
                Button2.ForeColor = Color.Green
            Else
                Button2.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.103") Then
                Button3.ForeColor = Color.Green
            Else
                Button3.ForeColor = Color.Red
            End If
            If My.Computer.Network.Ping("192.168.0.104") Then
                Button4.ForeColor = Color.Green
            Else
                Button4.ForeColor = Color.Red
            End If
        End Sub

    the code as above but the results are slow
    Sunday, February 16, 2020 12:00 PM
  • Read the following post (code is in C# but the important thing is the exploration not the code). Using their logic attempts went from 2 minutes to milliseconds. I can't try it out as I'm home not on a network today.

    https://www.justinmklam.com/posts/2018/02/ping-sweeper/

    The important code is here from their GitHub C# repository.

    Public Class PingSweep
        Private BaseIP As String = "192.168.1."
        Private StartIP As Integer = 1
        Private StopIP As Integer = 255
        Private ip As String
    
        Private timeout As Integer = 100
        Private nFound As Integer = 0
    
        Private Shared lockObj As New Object()
        Private stopWatch As New Stopwatch()
        Private ts As TimeSpan
    
        Public Sub RunPingSweep_Sync()
            nFound = 0
    
            stopWatch.Start()
            Dim p As New System.Net.NetworkInformation.Ping()
    
            For i As Integer = StartIP To StopIP
                ip = BaseIP & i.ToString()
                Dim rep As Net.NetworkInformation.PingReply = p.Send(ip, timeout)
    
                If rep.Status = Net.NetworkInformation.IPStatus.Success Then
                    nFound += 1
                End If
            Next
    
            stopWatch.Stop()
            ts = stopWatch.Elapsed
    
            MessageBox.Show(nFound.ToString() & " devices found! Elapsed time: " & ts.ToString(), "Synchronous")
        End Sub
    
        Public Async Sub RunPingSweep_Async()
            nFound = 0
    
            Dim tasks = New List(Of Task)()
    
            stopWatch.Start()
    
            For i As Integer = StartIP To StopIP
                ip = BaseIP & i.ToString()
    
                Dim p As New Net.NetworkInformation.Ping()
                Dim task = PingAndUpdateAsync(p, ip)
                tasks.Add(task)
            Next
    
            Await Task.WhenAll(tasks).ContinueWith(
                Sub(t)
                    stopWatch.Stop()
                    ts = stopWatch.Elapsed
                    MessageBox.Show($"{nFound} devices found! Elapsed time: {ts}", "Asynchronous")
                End Sub)
        End Sub
    
        Private Async Function PingAndUpdateAsync(ping As Net.NetworkInformation.Ping, ip As String) As Task
            Dim reply = Await ping.SendPingAsync(ip, timeout)
    
            If reply.Status = Net.NetworkInformation.IPStatus.Success Then
                SyncLock lockObj
                    nFound += 1
                End SyncLock
            End If
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, February 16, 2020 3:51 PM
    Moderator
  • Pings take time.  Remember, the only way you can determine that a system is unreachable is by timeout.  There is no return signal that says "I am not here".  If you send, and nothing comes back, that's how you know.

    You might consider having your timer tick routine rotate through the systems one at a time, so the first tick you check 101, the next tick you check 102, etc.  That way, each tick runs pretty quickly.  You don't need sub-second response time on something like this.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, February 16, 2020 11:50 PM
  • what about the tagging button?
    Monday, February 17, 2020 2:11 AM
  • what about the tagging button?

    Perhaps a super simple example which I deviated from the first code sample.

    Imports System.Net.NetworkInformation
    
    Public Class Form1
        Private ButtonItemList As New List(Of Item)
        Private Async Function PinAddress(address As String, timeout As Integer) As Task(Of Boolean)
            Try
                Dim ping As New Ping()
                Dim reply = Await ping.SendPingAsync(address, timeout)
                Return (reply.Status = IPStatus.Success)
            Catch
                Return False
            End Try
        End Function
        Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            For Each item As Item In ButtonItemList
                If Await PinAddress(item.Address, 300) Then
                    item.Button.BackColor = Color.White
                Else
                    item.Button.BackColor = Color.Red
                End If
    
            Next
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ButtonItemList.Add(New Item() With {
                                  .Button = Button1,
                                  .Address = "docs.microsoft.com"})
            ButtonItemList.Add(New Item() With {
                                  .Button = Button2,
                                  .Address = "104.112.178.21"})
            ButtonItemList.Add(New Item() With {
                                  .Button = Button3,
                                  .Address = "2607:f8b0:400a:800::200e"})
        End Sub
    End Class
    Public Class Item
        Public Property Button() As Button
        Public Property Address() As String
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ahda Muammar Monday, February 17, 2020 3:54 AM
    Monday, February 17, 2020 3:42 AM
    Moderator