Backgroundworker and my.computer.network.ping

Hello,

I am developing a small application that checks a list of ip's to see if they are reachable or not. When they aren't available the icon turns red, otherwise it just stays white.

I've decided to use a backgroundworker to check if the pc's are available when there are more then 0 computers in the list.

However when the program reaches the my.computer.network.ip("insert adres") event my program freezes until it gets a timeout or a reply. I thought that using a backgroundworker wasn't supposed to effect the winform itself.

Does anyone know how I can fix this problem

With friendly regards,

Gwen


Answer this question

Backgroundworker and my.computer.network.ping

  • Gladys27216

    Just as I suspected. You are calling Ping on the UI thread.

    The line:


    If lvPcs.InvokeRequired Then
     lvPcs.Invoke(New getpcnummerdelegate(AddressOf getpcnummer), New Object() {i})

     

    It is basically saying; 'if not on the user interface thread, then call this method on the user interface thread'. Which defeats the purpose of having the background worker in the first place.

    I would probably do something like this:


    Imports System.Net.NetworkInformation

    Public Class Form1

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim urls(ListView1.Items.Count - 1) As String

      For i As Integer = 0 To ListView1.Items.Count - 1

       urls(i) = ListView1.Items(i).Text

      Next

      BackgroundWorker1.RunWorkerAsync(urls)

     End Sub

     Private Sub OnBackgroundWorkerDoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

      Dim urls() = CType(e.Argument, String())

      For i As Integer = 0 To urls.Length - 1

       Dim success As Boolean = False

       Try
        success = My.Computer.Network.Ping(urls(i))
       Catch ex As PingException
       Catch ex As InvalidOperationException
       End Try

       BackgroundWorker1.ReportProgress(i, success)

      Next

     End Sub
     Private Sub OnBackgroundWorkerProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

      If e.UserState = True Then

       ListView1.Items(e.ProgressPercentage).ImageIndex = 0

      Else
       ListView1.Items(e.ProgressPercentage).ImageIndex = 1
      End If

     End Sub
    End Class


     



    This assumes that you have:

    1. A Button control named Button1
    2. A ListView control named ListView1
    3. A BackgroundWorker component named BackgroundWorker1



  • jjones11

    Can you post the code you are using I tried just this and it worked fine.

    Are you calling Ping in the DoWork handler

  • Chuck McD



        Private Sub Controlleur_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Controlleur.DoWork
            Dim i As Integer
            Dim delay As Integer
            delay = Convert.ToInt32(e.Argument)
            While lopen = True
                If aantalpcs > 0 Then
                    For i = 0 To aantalpcs - 1 Step +1
                        getpcnummer(i)
                        Controlleur.ReportProgress(0, i)
                    Next
                    draad.Sleep(delay)
                End If
            End While
        End Sub

     




        Private Sub getpcnummer(ByVal i As Integer)
            If lvPcs.InvokeRequired Then
                lvPcs.Invoke(New getpcnummerdelegate(AddressOf getpcnummer), New Object() {i})
            Else
                Dim adres As String = lvPcs.Items(i).Text
                Dim gevonden As Integer
                If My.Computer.Network.IsAvailable Then
                    If controle(adres) Then
                        gevonden = 0
                    Else
                        gevonden = 1
                    End If
                Else
                    gevonden = 1
                End If
                setstatus(gevonden, i)
            End If
        End Sub

     




        Private Function controle(ByVal adres As String) As Boolean
            If My.Computer.Network.Ping(adres, 0) Then
                Return True
            Else
                Return False
            End If
        End Function

     




        Private Sub setstatus(ByVal gevonden As Integer, ByVal i As Integer)
            If gevonden = 1 Then
                lvPcs.Items(i).ImageIndex = 1
            Else
                lvPcs.Items(i).ImageIndex = 0
            End If
        End Sub

     


    therre you go

  • Ivan Andrade

    Thanks for the explanation, it works perfectly now :)

    greets

  • Backgroundworker and my.computer.network.ping