简体   繁体   中英

C# WebBrowser handling when triggering a ComponentArt CallBack click/event

I am trying to navigate to a page with a hidden browser from within a C# application, click on a link and verify the click worked. Flow: - navigate to "http://mypage" - click on a link, the link HTML code is (calling a ComponentArt CallBack):

<div id="ctl00_ctl00_PlaceHolderMain_Colleague1_linkAdd" onclick="colleagueCallback.callback('SOMENAME'); return false;">

Add colleague - wait for the click to get the result and update the page - verify the click worked

My problem: The click does not seem to fully complete, unless I call the WebBroser.Print() (which I do not need, just tried a million ways to figure out what is going on after the click, only Print is making the click complete).

The code, which works absolutely fine, navigate is working, it can find the tags I am looking for, the click event is firing (I get an intermediary change), but the callback does not complete, as it turns out only using the WebBrowser.Print(), all the other WebBrowser.Invalidate(), WebBrowser.Update(), WebBrowser.DocumentStream.Flush() or WebBroser.ResumeLayout() won't finalize the ASP callback within the page ...:

tempIE = new WebBrowser();
tempIE.Navigate("http://mypage");
while (tempIE.ReadyState != WebBrowserReadyState.Complete)
{
      Application.DoEvents();
}
tempIE.AllowNavigation = true;
HtmlElement addDivTag = tempIE.Document.GetElementById("ctl00_ctl00_PlaceHolderMain_Colleague1_linkAdd");
if (addDivTag == null)
    throw (new Exception("Session.FollowPerson: could not find the Add tag"));
if ( addDivTag.FirstChild == null )
    throw (new Exception("Session.FollowPerson: could not find the add link"));
addDivTag.FirstChild.InvokeMember("click");
while (tempIE.ReadyState != WebBrowserReadyState.Complete)
{
    Application.DoEvents();
}

After this I am verifying if one of the tags has been changed by the ComponentArt server CallBack but only getting the intermediary tag. The change I am monitoring is:

<a href="javascript:void(0)">Add colleague</a>

will change intermediary to (while making the add logic in the CallBack):

<img src="/images/loading.gif">

in the end should change to:

<br>Is your colleague</br>

Anyone knows what exaclty is going on in the WebBroser.Print() that makes the page fully complete the callback ?

I believe WebBrowser does its work in a separate thread. Print would therefore use a thread synch mechanism, like a WaitXXX type function (which allows the message pump to keep running while causing the GUI thread to wait for an event).

The DoEvents may not be giving your thread a chance to run (are you using a single processor cpu?). Try inserting a Thread.Sleep(200) and see if the behavior changes. If so, consider changing to one of the WaitXXX functions (I'll look up the one I'm thinking of, and post an edit).

EDIT The were MsgWaitXXX functions and you would need to P/Invoke them if you choose to use them.

I would recommend you hook the WebBrowser.Navigated event. Break your logic into two parts. The first part would call Navigate and flag your UI as waiting for something, the second part would be done in your Navigated handler when the event fires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM