0

hi guys i have backgroundworker that updates picture box that is checking a ping and based on response time picks 1 of 6 different pictures to fill in the box ...kind of looks like a signal bar picture. I set the error image to be the a pic that looks like zero bars but for some reason out of no where it decides its gonna show a error pic of a red X and even tho when debugging i can see that based on the ping response it does tell it to load the proper pic from resources...well it doesn't change it. the background worker is triggered by a timer set to 1000 ms but have raised it as high as 5 seconds and still get error...going to include code and images if i can figure out how lol

Public Sub CheckNetwork()
    Dim Result As Net.NetworkInformation.PingReply
    Dim SendPing As New Net.NetworkInformation.Ping
    Dim ResponseTime As Long '<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Try
        Result = SendPing.Send("8.8.8.8")
        ResponseTime = Result.RoundtripTime
        If Result.Status = Net.NetworkInformation.IPStatus.Success Then
            If ResponseTime.ToString > 0 And ResponseTime.ToString < 35 Then
                pbInternetStatus.Load("resources\signal5.png")
            ElseIf ResponseTime.ToString >= 35 And ResponseTime.ToString < 45 Then
                pbInternetStatus.Load("resources\signal4.png")
            ElseIf ResponseTime.ToString >= 45 And ResponseTime.ToString < 65 Then
                pbInternetStatus.Load("resources\signal3.png")
            ElseIf ResponseTime.ToString >= 65 And ResponseTime.ToString < 85 Then
                pbInternetStatus.Load("resources\signal2.png")
            ElseIf ResponseTime.ToString >= 85 Then
                pbInternetStatus.Load("resources\signal1.png")
            End If
        Else
            pbInternetStatus.Load("resources\signal0.png")
        End If
    Catch ex As Exception
    End Try
End Sub

Private Sub bgwCheckNetwork_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles bgwCheckNetwork.DoWork
    CheckNetwork()
End Sub

Private Sub tmrNetCheck_Tick(sender As Object, e As EventArgs) Handles tmrNetCheck.Tick
    If Not bgwCheckNetwork.IsBusy Then
        bgwCheckNetwork.RunWorkerAsync()
        bgwCheckNetwork.Dispose()
    End If
End Sub

Image without any error :

Without the error

Image showing the error :

With the error

Kami
  • 19,134
  • 4
  • 51
  • 63
TotalNoob
  • 37
  • 8
  • First thing to do would be to comment out the `Try..Catch..End Try` lines. If you're getting some sort of error, the empty `Catch` block will just ignore it and carry on, probably resulting the problem in your question. – David Wilson Jul 03 '18 at 11:08
  • The first rule of `BackgroundWorker` is DO NOT do anything to any controls in the `DoWork` event handler. Also, why on Earth are you disposing the `BackgroundWorker` immediately after calling `RunWorkerAsync`? – jmcilhinney Jul 03 '18 at 11:10
  • 1
    I notice that you're comparing strings to numbers so there could be other errors which using [`Option Strict On`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement) would point out for you. – Andrew Morton Jul 03 '18 at 11:27
  • @DavidWilson thanks for your response David I will try that but since it only happens once in a while when I see it I use the debugger to step thru line by line at the beginning of that Try and I see that it actually still runs thru as expected ...I mean if the ping was 35 for example it then goes to the corresponding line to load the picture but the picture doesnt change anyways...I never hit the catch part...but will try and update you later – TotalNoob Jul 03 '18 at 11:30
  • @jmcilhinney Thanks for your response I was unaware that I shouldnt use it for controls...should I just run that in a timer or what would you suggest...I just didnt want it taking up to much time on my main thread. I thought the timer would run ascync do the main code and when finished dispose it....should I dispose at the end of the main code? – TotalNoob Jul 03 '18 at 11:36
  • The Red Cross of Failure tells you that the control's OnPaint() method threw an exception. A likely cause is calling the Load() method from a worker thread, that is not legal. Using a control from a worker thread usually throws an InvalidOperationException but not all corner-cases can be easily checked. Do this the right way, use the RunWorkerCompleted event. Calling the BGW's Dispose() method right after calling RunWorkerAsync() is also very, very wrong. But since it doesn't do anything you'll get away with that bug. – Hans Passant Jul 03 '18 at 13:04

0 Answers0