Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Interation Not Working
#1
I'm using a DO interation to loop a function I'm using to test for internet connectivity. The code is working fine, except that when one of the tests is satisfied the loop stops. I want this to continue in the background while the program is running. How can I get this to work?

Here's the code:
Code:
Private Sub checkInternet()
        
        Dim InetChecker As Boolean
        InetChecker = CheckForInternetConnection()
        Do While LabelCount.Text <> ""
            Thread.Sleep(10)
            If InetChecker = True Then
                Dim image = My.Resources.greenbar
                PictureBox4.Image = image

            Else
                Thread.Sleep(10)
                Dim image = My.Resources.redbar
                PictureBox4.Image = image
                'NoInetConnError.Show()
            End If
        Loop
    End Sub
//Kismet
#2
Well, you can use the BackGround Worker Control to loop through this code without having any effect on the appearance of the application at all.

(See more information about a background worker here: <!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=jv4FdoaUMQE&feature=plcp&context=C322e31eUDOEgsToPDskK9c_-OCENEeHwzWSO_Mojs">http://www.youtube.com/watch?v=jv4FdoaU ... wzWSO_Mojs</a><!-- m -->)

And since you want the loop to constantly be running, create a timer with the interval of 1000 or something like that, and have the timer tick event call the DoWork() Function of the BackgroundWorker
My Blog | My Setup | My Videos | Have a wonderful day.
#3
You can allso check out my video about Enum and Loops



<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=S3BH9CQAIN0">http://www.youtube.com/watch?v=S3BH9CQAIN0</a><!-- m -->
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#4
Guys,

Thanks for your help thus far. So I added a Timer control to the form and in its _Click event, I called the thread I'd already setup to handle the connection check.

All is working fine, except that now I'm getting the following exception:

Quote:System.InvalidOperationException was unhandled
Message=Object is currently in use elsewhere.

I guess the loop is working but now since the greenbar.png image file had already been displayed in the previous interation, it's saying the file is already open.

So question is, how do I release the file before going into another interation?

PS. Brandon, I haven't watched your video yet. I'm leaving work now, and will do that when I get home. But I suspect I will run into the same problem.
//Kismet
#5
To release a file, you may need to do the following:

Code:
PictureBox4.Image = Nothing
ImageName1.Dispose()
My Blog | My Setup | My Videos | Have a wonderful day.
#6
Brandon,

I've already tried that, doesn't work. I get another exception telling me that I cannot call close() on a button while dispose() has been called.

This happens because I use a button_click event to close my application, and this exception is thrown if I use the ImageName.dispose() method.

I'm starting to loose my hair.........joke.......
//Kismet
#7
Don't worry! We'll figure this out! If you could attach your whole source file to this thread, we can figure it out even faster!
My Blog | My Setup | My Videos | Have a wonderful day.
#8
Brandon,

I got some help from Stackoverflow.com () on how to use the BackgroundWorker and it all works fine. Here's the code I'm using:

Code:
Private connected As Boolean

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
     Handles BackgroundWorker1.DoWork
        While True
            Dim online = CheckForInternetConnection()
            If online <> connected Then
                connected = online
                BackgroundWorker1.ReportProgress(CInt(online))
            End If
            Thread.Sleep(500)
        End While
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
     Handles BackgroundWorker1.ProgressChanged
        Dim online As Boolean = CBool(e.ProgressPercentage)
        If online Then
            PictureBox4.Image = My.Resources.greenbar
        Else
            PictureBox4.Image = My.Resources.redbar
            Me.Hide()
            NoInetConnError.Show()
        End If
    End Sub
//Kismet
#9
Great! I am glad you got it working! For reference, could you link us to the Stack Overflow page that you used?

(Sorry that we were unable to provide sufficient help)
My Blog | My Setup | My Videos | Have a wonderful day.
#10
Brandon,

Don't sweat it man. You do a tremendous work here, and I'm learning a lot from your videos.

Here's the link to the Stackoverflow.com question/solution concerning this issue:

http://stackoverflow.com/questions/89174...68#8917768
//Kismet


Possibly Related Threads…
Thread Author Replies Views Last Post
  DataBase - Login - Not Working Murcs 4 16,068 07-02-2011, 09:46 AM
Last Post: brandonio21
  Working with Regex xolara 2 10,960 02-24-2011, 05:38 PM
Last Post: xolara

Forum Jump:


Users browsing this thread: 1 Guest(s)