Given this method of working with an HTML page in a web browser:
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 works like a charm when the message box is displayed, and not when it is commented out. My doubts may be renewed in the following questions:
1) as a code stream, when the message field is part of the program, and when not? I mean that the code locked before the user clicks normally?
2) phase I, indicated above with number 3, launches some javascript on a page that does not throw a navigation event (therefore not DocumentCompleted), but gives access to some hidden HTML that is not reachable without clicking on the tag A. In practice, this simply changes InnerHtml tag, creating a FORM in it.
3) I tried to implement several solutions for phase 4, a message box, as indicated here, ThreadSleep (), SpinWait () and even a cycle for messing all up, but all these solutions do not seem to allow Webbrowser to continue rendering the form on the screen. Only a message box displays it, even if the user very quickly clicks OK and closes it.
4) I need to find a solution that does not include external (user) input (such as Messagebox, which must be closed) in order to wait for the form to be displayed on the screen fully loaded, but no events will help.
Some more data to evaluate the case: - the code I wrote is good for the purpose, I tried to break it into 3 buttons to control the time manually, and it works fine. - the completed document cannot be used to switch between code splits, since about 300 pages are automated, and each page can have 10-15 methods for automating them, it is impossible to control one event handler for all of them without creating endless struct switch. I would try to avoid this if possible. - I found some interesting questions for other users, such as the following, but without a solution for my case:
InvalidCastException using WebBrowser.IsBusy or ReadyState (VB.NET)
Detect when AJAX changes HTML to DIV in WebBrowser
http://www.techtalkz.com/vb-net/374234-vb-net-webbrowser-control-how-capture-javascript-events-statusbar-changed-mouseclick-etc.html
Can someone give me a hand.
Sorry, this is my first thread, hope I was clean. Tks