简体   繁体   中英

webBrowser.DocumentCompleted Timing

How can we calculate timing? I mean when pressed click it's should start(time) and when finished load page than stop. when you searching in google, its shown you a time how long it takes. on image i show what i want

foreach (string bug in bugs)
{
    webBrowser.Navigate(new Uri(url));
    webBrowser.Document.GetElementById("product").SetAttribute("value", product);
    webBrowser.Document.GetElementById("version").SetAttribute("value", version);
    webBrowser.Document.GetElementById("commit").InvokeMember("click");

    //Need code to wait for page to load before continuing.
}

Google计时

Well it's pretty easy, first, NC = NavigateComplete Event, and DC = DocumentComplete Event, and WB is the WebBrowser1 (or WebBrowser) Control's name in your program.

Now, you basically get the number of ticks (or time) of the first NC event, and the ticks (or time) of the last DC event, and you subtract the NC time from the DC time. Also, when the NC is fired, you want to check in the NC event the pDisp property, and if it is equal to WB.object - if it is, this tells you it's the top-level document on the page, which is useful if there are frames since more than one would occur. Also, most importantly, the top-level document NC event always occurs first.

Secondly, the top-level documents DC event always occurs last, so you check to make sure the pDisp is the same object as WB.object in the DC event before you take your DC timing.

To do the check, in VB it's as follows (similar in C#):

Do this check and Save time in NC event, and do the same for the DC event while saving the time in a second global variable of course, so you can subtract them later.

NC Event:

If pDisp Is WB.object Then
  ' I use this API, but there are better ways to do this in .NET,
  ' such as DateTime.Now.
  tNCTime = GetTickCount() 
End If

DC Event:

If pDisp Is WB.object Then
  ' I use this API, but there are better ways to do this in .NET,
  ' such as DateTime.Now.
  tDCTime = GetTickCount()

  ' You can do this elsewhere, but here is fine too, since when this occurs,
  ' we know loading is done.
  tResult = tDCTime - tNCTime ' More details on this below...

End If

Declarations should go in a global module somewhere, depending on how your code is structured, of course, if they are all in the same namespace, no need to go global.

Dim tNCTime As DateTime
Dim tDCTime As DateTime
' Below Only if using DateTime.Now, declare as Integer if you're using Tick Counts.
Dim tResult As TimeSpan

Now, the advantage of doing the WB.object check is that it also tells you when the webpage is done loading :)... so in the DC event's IF Then End If check, you can place the calculation :). Cool?

If you use tick counts, its in milliseconds, so don't forget to divide by 1000 to get it in seconds, and ensure its saved as a double or a decimal since an integer or a long will cause an overflow.

Let me know if this has worked for you and if I have understood your question correctly. If there is anything else I can do to help, let me know. I love giving back to the people on this site that has helped me so much. (hola @Hans Passant if you're reading hehe).

PS: Also, as an aside, since you are a new user, please don't forget to accept as an "Answer" if this answers your question, so it will not come up under "Unanswered" section any longer.

why don't you use document.cookie=""; to store the request time for the page and on page load get the time form the stored cookie and compare with the current time to get the difference and display it

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