webBrowser.DocumentCompleted 计时

发布于 2025-01-04 03:45:35 字数 554 浏览 2 评论 0原文

我们如何计算时间?我的意思是,当按下点击时,它应该开始(时间),当完成加载页面时,而不是停止。当你在谷歌搜索时,它会告诉你需要多长时间。在图像上我显示了我想要的

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 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 Timing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2025-01-11 03:45:35

嗯,这很简单,首先,NC = NavigateComplete 事件,DC = DocumentComplete 事件,WB 是程序中 WebBrowser1(或 WebBrowser)控件的名称。

现在,您基本上可以获得第一个 NC 事件的滴答数(或时间)和最后一个 DC 事件的滴答数(或时间),然后从 DC 时间中减去 NC 时间。另外,当 NC 被触发时,您需要在 NC 事件中检查 pDisp 属性,并且它是否等于 WB.object - 如果是,这会告诉您它是顶级文档在页面上,如果有框架,这很有用,因为会出现多个框架。此外,最重要的是,顶级文档 NC 事件始终首先发生。

其次,顶级文档 DC 事件总是最后发生,因此在获取 DC 计时之前,您需要检查以确保 pDisp 与 DC 事件中的 WB.object 是同一对象。

要进行检查,在 VB 中如下所示(与 C# 中类似):

执行此检查并在 NC 事件中保存时间,并对 DC 事件执行相同的操作,同时将时间保存在第二个全局变量中,因此您可以减去他们稍后。

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

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

声明应该放在全局模块中的某处,具体取决于代码的结构,当然,如果它们都在同一命名空间中,则无需全局。

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

现在,进行 WB.object 检查的优点是,它还会告诉您网页何时完成加载:)...因此,在 DC 事件的 IF then End If 检查中,您可以进行计算:)。凉爽的?

如果您使用刻度计数,则以毫秒为单位,因此不要忘记除以 1000 以获取以秒为单位的值,并确保将其保存为双精度型或小数型,因为整数或长整型会导致溢出。

让我知道这是否对您有用以及我是否正确理解了您的问题。如果还有什么我可以帮忙的,请告诉我。我喜欢回馈这个网站上对我帮助很大的人们。 (你好@Hans Passant,如果你正在读的话呵呵)。

附:
另外,顺便说一句,由于您是新用户,如果这回答了您的问题,请不要忘记接受作为“答案”,这样它就不会再出现在“未回答”部分下。

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.

流心雨 2025-01-11 03:45:35

你为什么不使用
文档.cookie =“”;
存储页面的请求时间并在页面加载时从存储的 cookie 中获取时间并与当前时间进行比较以获取差异并显示它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文