简体   繁体   English

webBrowser.DocumentCompleted计时

[英]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. 当您在google中搜索时,它会向您显示一段时间。 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. 好吧,这很容易,首先,NC = NavigateComplete事件,DC = DocumentComplete事件,WB是程序中WebBrowser1(或WebBrowser)控件的名称。

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. 现在,您基本上可以获得第一个NC事件的滴答声(或时间)和最后一个DC事件的滴答声(或时间),然后从DC时间中减去NC时间。 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. 另外,在触发NC时,您要在NC事件中检查pDisp属性,如果它等于WB.object如果是,则告诉您它是页面上的顶级文档,这很有用如果有帧,因为可能会发生多个。 Also, most importantly, the top-level document NC event always occurs first. 同样,最重要的是,顶级文档NC事件始终总是首先发生。

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. 其次,顶级文档DC事件总是最后发生,因此在进行DC计时之前,请检查以确保WB.object与DC事件中的WB.object是同一对象。

To do the check, in VB it's as follows (similar in C#): 要进行检查,请在VB中进行如下操作(与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事件中保存时间,对DC事件进行相同的操作,同时当然将时间保存在第二个全局变量中,因此以后可以减去它们。

NC Event: NC事件:

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: DC事件:

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 :). 现在,执行WB.object检查的优点在于,它还可以告诉您网页加载完毕的时间:)...,因此在DC事件的IF If If End If检查中,您可以放置​​计算:)。 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. 如果您使用滴答计数,它以毫秒为单位,因此请不要忘记除以1000以秒为单位,并确保将其保存为双精度或十进制,因为整数或长整数会导致溢出。

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). (霍拉@Hans Passant,如果您正在阅读呵呵)。

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. PS:此外,由于您是新用户,因此如果回答了您的问题,请不要忘记接受作为“答案”,因此该问题将不再出现在“未回答”部分中。

why don't you use document.cookie=""; 你为什么不使用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 存储页面的请求时间并在页面加载时从存储的cookie中获取时间,并与当前时间进行比较以获取差异并显示出来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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