Solved

Wait Function in VB .NET??

Posted on 2006-10-28
5
1,639 Views
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.

Thanks
0
Comment
Question by:RaindogRob
5 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17827001
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.
0
 
LVL 10

Expert Comment

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

Expert Comment

by:ptakja
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.
0
 

Author Comment

by:RaindogRob
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
            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.
0
 
LVL 35

Accepted Solution

by:
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now