views:

382

answers:

1

Hi,       I had created a BHO application with the help of http://www.codeproject.com/KB/cs/Attach_BHO_with_C_.aspx?fid=447248&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51&select=2421069

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?

A: 

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%5FenUS330US330&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.

csharptest.net