当进度更改事件处理程序被触发时,如何减少 CPU 使用率?

发布于 2024-07-11 12:38:08 字数 1481 浏览 7 评论 0原文

我在 此链接

如果我构建上面文章中获得的源代码,CPU 使用率会增加到 70-80%。

我怎样才能减少这个?

在上面的文章中,而不是文档完整的事件处理程序,我使用了progresschange事件处理程序。

在 foreach 循环中,我曾经检查网页每个标签的标签名,在构建上述代码时(或注册 dll 后),CPU 使用率持续从 10% 到 80% 增加,如果存在,可能会导致问题是包含大量数据(元素)的网页.....

我想避免这种情况,是否有任何方法可以让我获取网页中存在的所有标签的所有标签名。 请提出一些建议,这样我就可以避免这个问题。 谢谢... 导致问题的代码以粗体显示。 对于找到的每个标签元素,它都会显示包含标签元素的标签名的消息框。

我遇到问题的代码是:

public void onProgressChange(int Progress, int ProgressMax) 
{    
    document = (HTMLDocument)webBrowser.Document; 
    foreach(IHTMLElement tempElement in (IHTMLElementCollection)document.documentElement.all)   
    {      
        System.Windows.Forms.MessageBox.Show(" Tagname:"+ tempElement.tagname);   
    }                 
}

public int SetSite(object site) 
{
    if (site != null)   
    {     
        webBrowser = (WebBrowser)site; 
        webBrowser.ProgressChange += new DWebBrowserEvents2_ProgressChangeEventHandler(this.onProgressChange);    
    }    
    else    
    { 
        webBrowser.ProgressChange = new DWebBrowserEvents2_ProgressChangeEventHandler(this.onProgressChange);
        webBrowser = null;     
    }

    return 0;
}

此事件重复生成。 如何降低CPU使用率?

I had created a BHO application with the help of this link.

If I Build the source code obtained in above article, the CPU usage is increased to 70-80%.

How can I reduce this?

In the above article, instead of Document complete event handler, i used progresschange event handler.

In the foreach loop, I used to check the tagname of every tag of a web page, while building the above code (or after registering the dll), the CPU usage was going on increasing from 10-80 percent which may cause problems if there is web page with lot of data(elements).....

I want to avoid this, Is there any method such that i can get all the tagnames of all the tag present in a web page.
Please suggest something such that i can avoid this problem.
Thanks...
The code which causes problem is in bold characters.
For each and every tag element found, It has display the message box containing the tagname of the tag element.

The code where I get problem is:

public void onProgressChange(int Progress, int ProgressMax) 
{    
    document = (HTMLDocument)webBrowser.Document; 
    foreach(IHTMLElement tempElement in (IHTMLElementCollection)document.documentElement.all)   
    {      
        System.Windows.Forms.MessageBox.Show(" Tagname:"+ tempElement.tagname);   
    }                 
}

public int SetSite(object site) 
{
    if (site != null)   
    {     
        webBrowser = (WebBrowser)site; 
        webBrowser.ProgressChange += new DWebBrowserEvents2_ProgressChangeEventHandler(this.onProgressChange);    
    }    
    else    
    { 
        webBrowser.ProgressChange = new DWebBrowserEvents2_ProgressChangeEventHandler(this.onProgressChange);
        webBrowser = null;     
    }

    return 0;
}

This Event is generated repeatedly. How to reduce CPU usage?

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

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

发布评论

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

评论(2

凌乱心跳 2024-07-18 12:38:08

这是一个没有答案的问题。 您的 CPU 使用率问题是由您的操作造成的,而不是由您的操作方式造成的。 IE 是一只狗,尤其是当你遍历所有集合时。 请记住,每个对象都必须编组到 .Net 中才能访问它。 我建议您以另一种方式解决您的问题,或者使用 IE 以外的解析器来处理 HTML。 您可以使用 WebClient 类加载 HTML,然后将结果提供给您喜欢的任何解析器。 运行简单的谷歌搜索将出现几个替代解析器:

http://www.google.com/search?hl=en&rlz=1C1GGLS_enUS330US330&q=html+parser+C%23+.net& ;aq=f&oq=&aqi=

如果出于某种原因您无法使用 IE,您需要在 document.all 集合上找到 foreach 语句的替代答案。

This is a question without an answer. Your CPU usage problem is a result of what your doing, not so much how. IE is a dog, especially if you walking the all collection. Remember each and every object has to be marshaled into .Net for you to access it. I would recommend you approach your problem another way, or use a parser other than IE to process the HTML. You can use the WebClient class to load the HTML, then feed the result to whatever parser you like. Running a simple google search will turn up several alternative parsers:

http://www.google.com/search?hl=en&rlz=1C1GGLS_enUS330US330&q=html+parser+C%23+.net&aq=f&oq=&aqi=

If for whatever reason your stuck with IE, you need to find an alternative answer to the foreach statement on the document.all collection.

看透却不说透 2024-07-18 12:38:08

此事件会重复生成。 如何降低CPU占用率?

根据 webBrowser 的实现,函数 onProgressChange 可能会被非常快地调用。

尝试重写 onProgressChange 以跳过某些调用并偶尔执行实际工作,例如每秒一次。

This Event is generated repeatedly. How to reduce CPU usage?

Function onProgressChange can be potentially invoked very very fast depending on webBrowser implementation.

Try rewriting onProgressChange to skip some of it's calls and to do actual work once in a while, for example once per second.

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