DocumentNode.Selected Nodes Causes Context Switch Deadlock

Topics: User Forum
Dec 8, 2010 at 10:23 PM

When scraping this particular set of pages, my program hangs and eventually throws a Context Switch Deadlock If I save the same page to my file system and run the scrape it works fine. When I pull the page into a string directly from a WebBrowser object, use browser1.DocumentText.ToString(), and send it as a string to the exact same function I receive the error. There are no differences between the two html files that I can see. Has anyone ever encountered this problem before? Here is the calling function as well as the scrape function.

//Calling Code

DataTable table2 = PageScrapes.scrapeLpsAsap(browserDisplay.DocumentText.ToString());

 

//Receiving code

public static DataTable scrapeLpsAsap(string page)
        {
            DataTable table = DataBuilders.LpsAsap();
            string output = String.Empty;
            string[] columnValue = new string[3];
            int controlValue = 1;
            int recordCount = 0;

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.OptionDefaultStreamEncoding = Encoding.UTF8;
            doc.Load(new StringReader(page));
            try
            {
                int evaluate = doc.DocumentNode.SelectNodes("//tr[@class='GridItem']//td//table//tr//td[2]").Count; <----------- THROWS UP HERE. Does not throw an exception. Just hangs
                //Scrapes Odd Rows
                foreach (HtmlNode cell in doc.DocumentNode.SelectNodes("//tr[@class='GridItem']//td//table//tr//td[2]"))
                {

                    if (controlValue.Equals(11))
                    {
                        columnValue[recordCount] = cell.InnerText.Trim(); recordCount++; //Add opening bid
                        columnValue[recordCount] = "LPS"; //Add scrape identifier
                        DataBuilders.LpsAsapRow(table, columnValue);
                        controlValue++; recordCount++; //increment counters
                    }
                    else if (controlValue.Equals(6))
                    {
                        columnValue[recordCount] = cell.InnerText.Trim(); //add apn as first value
                        controlValue++; recordCount++; //increment counters
                    }
                    else if (controlValue.Equals(15)) { controlValue = 1; recordCount = 0; } //reesets control values
                    else { controlValue++; }
                }//End try