Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello to all of you , and so welcome;

I want thread wait for web until web done.i used this code:



VB
Dim Thread2 As System.Threading.Thread


   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        CheckForIllegalCrossThreadCalls = False

    End Sub


 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Thread2 = New System.Threading.Thread(AddressOf Ros)
        Thread2.IsBackground = True
        Thread2.Start()
    End Sub


Private Sub Ros()
        For Number_of_page = 1 To 10

            WebBrowser1.ScriptErrorsSuppressed = True
            WebBrowser1.Navigate("http://www.codeproject.com/script/Answers/List.aspx?tab=active&alltags=True&tags=842&pgnum=" & Number_of_page)

             While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While

            MsgBox("Page loaded!")

        Next Number_of_page

    End Sub

But in thread become an unknown error.i don't why?
Posted
Comments
phil.o 21-Sep-13 18:03pm    
Sorry, but unknown error -> unknown solution...
[no name] 21-Sep-13 18:13pm    
Most likely because you are trying to play with UI controls from a non UI thread.

1 solution

Well, I can say that I spot a big problem from the beginning: The CheckForIllegalCrossThreadCalls should not be set to false.

But then your Application.DoEvents will not work, right? Well, you should never call Application.DoEvents from a secondary thread (even on a primary thread it is not a good practice to use it).

In fact, I am not sure about the component that you are using and if it can be used from secondary threads. I really believe such WebBrowser component has a PageLoaded or similar event that you shoud use as it is the most common practice in most webbrowser components, and so you don't "wait" for the page to be loaded, you use the right event.
 
Share this answer
 
Comments
omid.r.a.candy 21-Sep-13 18:29pm    
Thank you so much.My friend i know what you told me about:

1-Application.DoEvents ; well i know that we have to Different thread ,but i wanted to told you what i want.i mean is the thread wait for WebBrowser as long as WebBrowser Completed.

2-event; well we have some event for.but don't want to use it.
Paulo Zemek 21-Sep-13 18:54pm    
So, in such particular case, your problem is still the checkforillegalcrossthreadcalls. In fact, such test should be kept, but you should send a message to the control in the while. I think you are using WindowsForms and I don't use it actually, but I believe you could use a WebControl.Invoke(TheMethodThatDoesTheStatusValidation) instead of checking the state directly...

But if you really want the simplest solution, replace the Application.DoEvents (which is completely incorrect) by a Thread.Sleep(10) for example. So, at each 10 milliseconds you check the state again.
omid.r.a.candy 21-Sep-13 20:02pm    
well Mr.Zemek using Sleep(~) not a good idea! because we don't know how much time needed to page done.i mean using sleep not Engineering way to solve this Problem.

I run up code without Tread and it work very well but my Win.form program Glaciated! or Hanged.So i Decided use thread.

please tell me more about WebControl.Invoke(TheMethodThatDoesTheStatusValidation) and give some code for vb.net or C# to test it.

And i am So glad to Help me.
Paulo Zemek 21-Sep-13 22:39pm    
Application.DoEvents does not wait at all. Considering it worked from another thread, that only means you will consume 100% of one CPU while the ReadyState is not complete. So, I gave a solution (which I consider a bad solution) that keeps the idea of using another thread instead of using the correct event. In such case, you can either ignore DoEvents completely (so, remove that line, but use the same while to keep waiting) or, if you don't want to consume lot of CPU waiting but still wants something responsive, you use a Thread.Sleep of at least 1 millisecond (which, in practice, will probably be something near 15 milliseconds as that's how windows usually works).

About the WebControl.Invoke... what I can say is this: If you don't do cross-thread calls, you must "ask" the main thread to execute some code... that's done with Control.Invoke (so, Window.Invoke, button.Invoke or similar). But I can't simply tell you how to write it in VB.NET because I am not really used to VB.NET... I can write the code in C# with ease.

Yet, I don't understand the purpose of this, as the best solution will be to use the PageLoaded/Completed event or similar.
Sergey Alexandrovich Kryukov 21-Sep-13 21:29pm    
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900