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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

697 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