Link to home
Start Free TrialLog in
Avatar of RaindogRob
RaindogRob

asked on

Wait Function in VB .NET??

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.

Thanks
Avatar of Raynard7
Raynard7

Hi,

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 http://www.devx.com/DevX/10MinuteSolution/20365/1954?pf=true has a pretty good descripton about how to do this.
use system.thread.sleep(1000) ' will hold for 1 second
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.
Avatar of RaindogRob

ASKER

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
            ThreadArray(Count).Start()
        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.

Thanks

I can resubmit this as another question for 500 pts if you like.
ASKER CERTIFIED SOLUTION
Avatar of Raynard7
Raynard7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial