Get ReadyState from WebBrowser control without DoEvents

前端 未结 3 2069
天命终不由人
天命终不由人 2020-11-30 10:29

This has been awnsered many times here and at other sites and its working, but I would like ideas to other ways to:

get the ReadyState = Complete after using a navig

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 11:26

    The solution is simple:

        // MAKE SURE ReadyState = Complete
                while (WebBrowser1.ReadyState.ToString() != "Complete") {
                    Application.DoEvents();         
                }
    

    // Move on to your sub-sequence code...


    Dirty and quick.. I am a VBA guys, this logic has been working forever, just took me days and found none for C# but I just figured this out myself.

    Following is my complete function, the objective is to obtain a segment of info from a webpage:

    private int maxReloadAttempt = 3;
        private int currentAttempt = 1;
    
        private string GetCarrier(string webAddress)
        {
            WebBrowser WebBrowser_4MobileCarrier = new WebBrowser();
            string innerHtml;
            string strStartSearchFor = "subtitle block pull-left\">";
            string strEndSearchFor = "<";
    
            try
            {
                WebBrowser_4MobileCarrier.ScriptErrorsSuppressed = true;
                WebBrowser_4MobileCarrier.Navigate(webAddress); 
    
                // MAKE SURE ReadyState = Complete
                while (WebBrowser_4MobileCarrier.ReadyState.ToString() != "Complete") {
                    Application.DoEvents();         
                }
    
                // LOAD HTML
                innerHtml = WebBrowser_4MobileCarrier.Document.Body.InnerHtml;  
    
                // ATTEMPT (x3) TO EXTRACT CARRIER STRING
                while (currentAttempt <=  maxReloadAttempt) {
                    if (innerHtml.IndexOf(strStartSearchFor) >= 0)
                    {
                        currentAttempt = 1; // Reset attempt counter
                        return Sub_String(innerHtml, strStartSearchFor, strEndSearchFor, "0"); // Method: "Sub_String" is my custom function
                    }
                    else
                    {
                        currentAttempt += 1;    // Increment attempt counter
                        GetCarrier(webAddress); // Recursive method call
                    } // End if
                } // End while
            }   // End Try
    
            catch //(Exception ex)
            {
            }
            return "Unavailable";
        }
    

提交回复
热议问题