Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:36:27+00:00 2026-06-07T00:36:27+00:00

Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;

  • 0

Given this method to work on a HTML page in a webbrowser:

    bool semaphoreForDocCompletedEvent;

                private void button12_Click(object sender, EventArgs e)
                        {
                            checkBox1.Checked = false; //unchecked if the NAvigating event is fired and Checked after DocumentCompleted is fired, only to have a visual reference on the Form
                            HtmlDocument doc = Program.wb.Document;
                            HtmlElement ele = doc.GetElementById("menuTable");
                            foreach (HtmlElement sub in ele.All)
                            {
                                if (sub.GetAttribute("href").Contains("something"))
                                {
                                    ele = sub;
                                    break;
                                }
                            }
//PHASE 1: clicking on a Web link to navigate to a page that contains other buttons and links                       object obj = ele.DomElement;
                            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
                            mi.Invoke(obj, new object[0]);
//PHASE 2: Waiting for document completed in order to be sure the document is fully loaded

                            semaphoreForDocCompletedEvent = WaitForDocumentCompleted();
                            if (!semaphoreForDocCompletedEvent)
                                throw new Exception("casino in giro!");

                            ele = doc.GetElementByI("button1").FirstChild.FirstChild.FirstChild.NextSibling;
//PHASE 3: clicking on a Web button to open a form

                            obj = ele.DomElement;
                            mi = obj.GetType().GetMethod("click");
                            mi.Invoke(obj, new object[0]);
//PHASE 4: displaying a modal MEssageBox that annoy the user a lot

                            if (checkBox1.Checked == false)
                                MessageBox.Show("non c'è stato document completed");
                            checkBox1.Checked = false;

//PHASE 5: submitting the form (that does not need any imput to be filled in)

                            ele = doc.GetElementById("planet");
                            ele = ele.FirstChild.NextSibling.NextSibling;

                            obj = ele.DomElement;
                            mi = obj.GetType().GetMethod("submit");
                            mi.Invoke(obj, new object[0]);
                        }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                Program.toBox = Program.wb.Document.Body.InnerHtml.ToString();
                if (Program.wb.ReadyState == WebBrowserReadyState.Complete)
                {
                    checkBox1.Checked = true;
                    IsBusy = false;
                }
            }

        private bool WaitForDocumentCompleted()
                { 
                    while (IsBusy)
                    {
                        Application.DoEvents();
                        Thread.SpinWait(1000);
                    }
                    return true;
                }

I need to understand why this code runs like a charm when the messagebox is displayed and does not when it is commented out.
My doubts can be resumend in these questions:

1) how is the flow of the code when the message box is part of the program and when it is not? I mean is the code blocked up to the user presses ok?

2) the phase I indicated above with number 3 fires some javascript in the page that does not issue a Navigating event (therefore no DocumentCompleted) but gives access to some hidden HTML not reachable without clicking on a A tag. In practice it just changes the InnerHtml of a tag, creating a FORM in it.

3) I tried to implement several solutions for phase 4, a Message box as indicated here up, a ThreadSleep(), a SpinWait() and even a for loop messing everything up, but all those solutions seem not to let the Webbrowser proceeding in visualizing the form on screen. Only the message box brings it up to screen, even if the user is very fast in pressing OK and closing it.

4) I need to find a solution that does not involve external (user) input (such the Messagebox to be closed) in order to wait for the form to appear completerly loaded on the screen, but no events come to help.

Some more data to evaluate the case:
– the code I wrote is good for the aim, I tried to split it into 3 buttons to manage the timing by hand and it works fine.
– the document completed cannot be used for switching between code splits, as there are around 300 pages automated and each page can have 10-15 methods to automate them, it’s impossible to manage a single eventhandler for all of them, without builind up a neverending Switch struct. I would try t avoid it if possible.
– i’ve found some interesting issues of other users like the following but without solution for my case:

InvalidCastException with WebBrowser.IsBusy or ReadyState (VB .NET)

Detect when AJAX changes HTML in a DIV in WebBrowser

http://www.techtalkz.com/vb-net/374234-vb-net-webbrowser-control-how-capture-javascript-events-statusbar-changed-mouseclick-etc.html

Could somebody give me a hand.

Sorry it is my first thread, hope I’ve been clear. Tks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:36:28+00:00Added an answer on June 7, 2026 at 12:36 am

    I’m posting here the solution i’ve been able to find for this problem: I wrote an extension method for the HtmlElement type as follows:

    public static bool WaitForAvailability(this HtmlElement tag, string id, HtmlDocument   documentToExtractFrom, long maxCycles)
    { 
        bool cond = true; long counter = 0; 
            while (cond) 
            { 
            Application.DoEvents(); 
            tag = documentToExtractFrom.GetElementById(id);
             if (tag != null) 
                cond = false;
             Thread.SpinWait(50000);
             counter++; 
            if (counter > maxCycles) 
                return false; 
            } 
        return true;
     }
    

    This allows the required tag to be waited for up to the moment when it will be really available in the page

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this class public partial class Default : Page { private IRepository repo; ...
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given this object class Contact { public IEnumerable<Person> People { get; } public string
I have a page that uses jQuery's $.post() method to get some html and
Example: I have a selector like this, which I give to another method as
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
Given this Itemgroup: <ItemGroup> <Foo Include=First> <Value>1</Value> </Foo> <Foo Include=Second> <Value>2</Value> </Foo> </ItemGroup> How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.