How can I reduce CPU usage when running a progress change event handler?

I created a BHO app using this link .

If I create the source code obtained in the previous article, the CPU load increases to 70-80%.

How can I reduce this?

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

In the foreach loop, I used tag validation of each webpage tag when generating the above code (or after registering the dll), CPU usage continued from 10 to 80 percent, which can cause problems if there is a web page with a lot of data (elements). ....

I want to avoid this, is there any method so that I can get all the tags of the entire tag present in the webpage. Please suggest something so that I can avoid this problem. Thanks ... The code causing the problem is in bold. For each tag element found, it displays a message box containing the tag of the tag element.

Code where I am getting the problem:

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 re-generated. How to reduce CPU usage?

0


source to share


2 answers


This is an unanswered question. The CPU usage problem is a result of what you are doing, not the way it is. IE is a dog, especially if you go all over the collection. Remember that every object must be placed in .Net in order for you to access it. I would recommend that you approach your problem differently or use a non-IE parser to handle HTML. You can use the WebClient class to load the HTML and then feed the result to any parser you like. Running a simple google search will bring 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 any reason you are stuck with IE, you need to find an alternative answer to the foreach statement in the document.all collection.

0


source


This event is re-generated. How to reduce CPU usage?

The function onProgressChange

can potentially be called very quickly depending on the implementation webBrowser

.



Try to rewrite onProgressChange

it to skip some of its calls and do the actual work once per second, for example once per second.

0


source







All Articles