Wait Function in VB .NET??

Posted on 2006-10-28
Last Modified: 2008-02-01
Anyone know of a wait function I can use in vb .NET to pause the program while I wait for my (form based) web browser to load (i.e. to reach readystate_complete)?

I tried this:
 - create a form class (with a webbrowser on it)
 - create a bool variable in this class called load_complete (this boolean gets set to true for each instance of the class when that instance's WebBrowser document_complete event gets fired)
 - create a class called DoStuff which inherits the form class
 - within a sub in the DoStuff class, create a form object and navigate its axWebBrowser1 to some url
 - send this sub into a loop waiting for the complete_load property of the form object to be set to true.........Do While form.complete_load <> true Sleep(100) Loop
 - the idea was that when document_complete gets fired for the instance of the form, that instance's load_complete property would get set to true and the DoStuff sub's loop would be exited leaving that sub to move on with its life

There is a problem with this approach in that sleep(100) seems to kill all processing in the program which means the document_complete event of the form classes browser doesnt get fired (I dont even think the webbrowser makes it to the page, it just hangs).

I need something that pauses the program but will not stop document_complete getting fired when the webbrowser has succesfully loaded the page.

Question by:RaindogRob
LVL 35

Expert Comment

ID: 17827001

this seems like a solution where threading would be optimal.

I would create one thread for the loading of this document and another to check its progress.
I would have the main thread (checking for the progress of the other) to call its thread.sleep method but this should allow all other threads to still operate.

You could also use a mutex - ie create worker thread to load the web browser - then get it to enter a protected mutex - then have your main thread enter the mutex,
then only when the other thread has finished does it release the mutex and your main thread can continue.

If you are totally lost with threading has a pretty good descripton about how to do this.
LVL 10

Expert Comment

ID: 17827113
use system.thread.sleep(1000) ' will hold for 1 second
LVL 14

Expert Comment

ID: 17830877
Raynard7 has the solution. Threading is the way to go.

Gangwisch, your solution will cause the CURRENT thread to sleep, thus ALL processing on that thread will be suspended for the duration specified leaving the author in the same boat.

Author Comment

ID: 17846212
Thanks Raynard7, sounds like threading will do the trick (the article you recommended was pretty clear too). One problem though, I've put together a program that works like this:

Sub DoStuff()
        Dim somearray(10) As classname
        Dim ThreadArray(10) As System.Threading.Thread

        For Count = 1 To 10
            array(Count) = New classname
            ThreadArray(count) = New System.Threading.Thread(AddressOf otherclass(count).DelegateFunctionName)
        Next Count

        For Count = 1 To 10
        Next Count
End Sub

The problem is this:
 - After the threads in ThreadArray have all been started, I need the DoStuff Sub to pause/sleep (leaving all the threads in the ThreadArray to run to completion) - I cant have DoStuff continuing until a bool property of the second class is set to true (this happens in the delegate function)
 - I have tried putting System.Threading.Thread.CurrentThread.Sleep(1000) at the end of the above code but that seems to freeze the entire program (i.e. even the ThreadArray threads seems to stop running with this).
 - To test this, I have put a msgbox in to alert me when each individual thread has finished executing (at the end of the delegate function). When I start each of the threads (as in the code above) and then use System.Threading.Thread.CurrentThread.Sleep(10000) as the final line of code in the dostuff sub, I would expect to see each delegate function msgbox pop up before the DoStuff sub is exited (i.e. before the Currentthread.Sleep(10000) has completed). This is not the case - instead the delegate function msgboxes are not coming up until the DoStuff System.Threading.Thread.CurrentThread.Sleep(10000) line has completed.

This is causing me a huge headache as it makes it look like the ThreadArray threads are not running concurrent with the DoStuff sub.

I'd really appreciate any help you can give me.


I can resubmit this as another question for 500 pts if you like.
LVL 35

Accepted Solution

Raynard7 earned 500 total points
ID: 17848198
Hi Rob, sorry have been busy all day - I'd suggest that you create another post to ask your new question

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help making a program or form for inventory that exports to txt 8 43
Disable extension 8 48
VB.NET 1 28
Visual studio 2015 1 21
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question