Results 1 to 13 of 13

Thread: Wait while webpage and source html load before going to next sub

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78

    Wait while webpage and source html load before going to next sub

    Hello,

    hmmmmmmmmmmmmmm...

    I have a sub that calls a number of other subs one after the other. One of the subs it calls is to load a particular webpage into a webbrowser and use the downloadComplete (cheers dynamic_sysop ) to load the source, obviously once the webpage has finished loading. After this there are a number of other subs to be called but the above two things have to be totally finished beforehand.

    The problem is it goes straight through and doesn't wait for the webpage and source to be loaded (just calls the subs for these and carries on) . I can understand that this is what it should do, but I want it to hold off the exciting prospect of running the next sub until all webpage/source html loading has finished.

    How to do this, well I thought if I put a boolean var in the downloadcomplete that becomes true when hit and the have a while not true loop in before the next sub it might work but it just loops infinitely and forgets the old webpage/source download that its s'posed to be doing. I tried putting a 'System.Threading.Thread.Sleep(1)' in the loop to see if that made it remember to do a bit of the downloading thing while looping but anyone with half a brain cell can see that I'm going kwayzeee and just trying nonsense ideas .

    Please could someone help me? I just want the program to wait until everything is downloaded before going to the next sub
    Last edited by CJN; Jul 17th, 2003 at 11:15 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Ive tried this: (brweb = webbrowser)
    VB Code:
    1. While brWeb.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
    2.             Sleep(4000) ' Wait 4 seconds.
    3.         End While

    But it is always readystate_loading.

    Ive tried:

    VB Code:
    1. Dim Thread1 As New System.Threading.Thread(AddressOf loadURLSource)
    2.                     Thread1.Start()
    3.  
    4.                     Thread1.Join()
    5. 'the rest of the subs

    and tried to understand what is happening, but it just fires the commands in loadURLSource and then carries on.

    Why wont it wait til the webpage and source has loaded?

    Last edited by CJN; Jul 17th, 2003 at 05:06 AM.

  3. #3
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    This is a very basic idea, you could download the page to a text file and check for the last tag i.e. '</html>', until you find a proper solution.
    Just a thought.
    Wind and waves resolves all problems.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Thanks for your help.

    I am displaying the source html in a textbox and checking to see if it has been filled. I think thats similar to the idea you suggested. The problem seems to be that anything that I do after calling the sub to download the webpage and source, for example check that the textbox is filled with the source code or start my next sub or do all that waiting stuff above stops my program bothering downloading the webpage and so it never downloads.

    I know that if I remove everything after calling the sub to download the webpage and source then the webpage and source is downloaded and displayed in the webbrowser and textbox, because the program has nothing else to do but download webpage/source.

    In effect I need to recreate the fact that the program thinks it has nothing else to do but download the webpage/source until it has done that and then make it carry on with the rest of my subs.

    Hope you guys understand and have some ideas

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    np cjn , right , are you calling the "other subs" from your Document_Complete sub ?
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Hi,

    No i'm not doing that. It goes a little something like this:

    VB Code:
    1. Private Sub MAINSUB()
    2.  
    3. 'Do some other stuff, including starting a loop
    4.  
    5. 'Call load web/source
    6. loadURLSource()
    7.  
    8. 'Call next lot of subs
    9. subC()
    10. subD() 'etc
    11. 'do some other stuff and go back to loop start
    12.  
    13. End SUb
    14.  
    15.     Private Sub loadURLSource()
    16.         'Load the url and the matching html source code
    17.         'Load the url to the webbrowser control brWeb...
    18.         brWeb.Navigate(curURL)
    19.      '   While brWeb.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
    20.         '    Sleep(4000) ' Wait 4 seconds.
    21.      '   End While
    22.         'Auto Call to the webbrowsers docComplete sub (through the doc) to load the source html when the url has finished...
    23.     End Sub
    24.  
    25.     Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    26.  
    27.  
    28.     Private Sub doc_DownloadComplete() Handles doc.DownloadComplete
    29.         'Load the source html...
    30.         txtSource.Text = brWeb.Document.documentelement.outerhtml
    31.       '  While loadcompl <> True
    32.       '      If txtSource.Text = "" Then
    33.       '          Sleep(4000) ' Wait 4 seconds.
    34.       '      Else
    35.       '          loadcompl = True
    36.       '      End If
    37.       '  End While
    38.  
    39.         MsgBox("doc")
    40.     End Sub

    Do you think ur idea would work, if its the only way then I will do it but putting all the rest of my subs and stuff in there is a but ugly and annoying. Cheers for the suggestion though. (you're almost becoming my VB gardian angel )

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    could you not do something like this :
    VB Code:
    1. Private Sub doc_DownloadComplete() Handles doc.DownloadComplete
    2.  
    3.         Try
    4.             Dim htm As String = brWeb.Document.documentelement.outerhtml
    5.             MessageBox.Show(htm)
    6.         Finally
    7.             '/// call subC() & subD()
    8.         End Try
    9.  
    10.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Well thankyou vey much dynamic_sysop . You idea seems to work, though I don't understand it. What the "try>finally>end try" actually does makes me and but gives me . Unlike webbrowsers which generally make me

    Cheers!

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Ohhhhhhhhhhhhhhhhhhhh!!!

    Thinking about it and doing a few tests, I'm afraid as good as your idea was its not going to work for me. I have to have my subs and other stuff where they started off (not in the download complete section) in order for other parts of my program to work. Darn, there must be some way of allowing it to pause while the webpage is loading and then carrying on, there just must , please let there be

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    what are you building? does the html have to arrive before an event triggers?
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Hi,

    Thanks with all assistance so far...

    What I am trying to do is something like the following:
    1) Start loop A running through a db looking for particular URLs
    2) Start loop B looking at particular things in URL
    3) Load webpage X (URL taken from db)
    4) Get source of X and put in txtbox
    5) Read some of Source X and put in record in database
    6) Read part of record and collate with other info
    7) Put all this in a lstvw, do some totalling and put in txtboxes
    8) Go back to 2) and find next particular thing (repeat til all things found)
    9) Go back to 1) look for Webpage Y, (repeat until webpage N)
    10) Display the lstvw and txtboxes to user.

    The problem is mainly my loops (starting at 1 through to 9 and 2 to 8). I can put steps 5) and 6) and possibly 7) in the download_complete sub but 8) and 9) are the "next" bits of a "for loop" these cannot go in the download_complete and not having them there yet having 5, 6, 7 there means the program seems to run something like the following:
    Steps..
    1>2>3>8>9>10>4>5>6>7 (probably not quite that but definitely wrong anyway). I know it definitely calls 10 before the webpage/source is displayed in their webbrowser/textbox because a messagebox is displayed found in sub 10 and the webpage hasn't even been displayed????

  12. #12
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    right , well firstly i wouldn't use a webbrowser to get the html , System.Net.WebClient is built for that .
    you could do it all within 1 sub if you want to read through an access db and get the url's sources that corrispond with those url's in the db , to get the source you use WebClient and StreamReader ( i always use this method , it's really fast compared to a browser )
    here's an example that might inspire you :
    VB Code:
    1. Imports System.Net
    2. ' /// ^^^ top of form's code page ...
    3. '/////////////////////////////////////////////
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim strUrls() As String = {"http://google.com", "http://www.vbforums.com"}
    7.         Dim webservice As New WebClient()
    8.         Dim x As Integer
    9.  
    10.         For x = LBound(strUrls) To UBound(strUrls)
    11.             Dim s As IO.StreamReader = New IO.StreamReader(webservice.OpenRead(strUrls(x)))
    12.             MsgBox(s.ReadToEnd) ' /// lets see the source code.
    13.             s.DiscardBufferedData()
    14.             s.Close()
    15.             s = Nothing
    16.         Next
    17.  
    18.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  13. #13

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Well thankyou dynamic_sysop, you have been very helpful. You suggestion may well become very useful to me in the future, because for now I have to stick with the webbrowser for reason that would take too long to explain, but I do indeed hope to get rid of it once some other issuses are sorted (making for a faster program, like you say). In the meantime I have a solution that seems to work. It is to put "Application.DoEvents" in a loop that checks if the ready state of the webbrowser has finished loading and hey presto loaded webpages and results

    Cheers for all your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width