views:

172

answers:

2

i am running 12 threads .function which these threads are calling is not having any lock on some object.but still these threads are taking too much time.(16 minutes).each thread ially parsing a xml document which is running alright if run indiviually.is this problem because of some reason related max no threads provided by express edition or some blocking dn by express edition.


code of func whic each thread calls is given below. each thread is given different rssfeed(urladdress)

public static class RssFileReader
{
    public static Rss GetRssDocumentData(string rssFeed)
    {
        Console.WriteLine("thread in RssFileReader: " + Thread.CurrentThread.Name);

        IFormatProvider culture = new CultureInfo("fr-FR", true);

        Rss rssDocumentObject=new Rss();
        XmlDocument documentObj = new XmlDocument();
        try
        {
            documentObj.Load(HttpClient.GetWebResponse(@rssFeed, null, null, 1200000, @"http://www.sahil.com").GetResponseStream());
        }
        catch(Exception e)
        {
            e.Source = "RssFileReader:Loading xmldocument object";
            throw;
        }
        try
        {
            XmlNodeList channelList = documentObj.GetElementsByTagName("channel");
            for (int k = 0; k < channelList.Count; k++)
            {

                rssDocumentObject.ListOfChannel.Add(new Channel());
                int noOfItemInChannel = -1;
                //XmlNodeList itemList = channelList[k].ChildNodes;
                for (int i = 0; i < channelList[k].ChildNodes.Count; i++)
                {


                    switch (channelList[k].ChildNodes[i].Name)
                    {

                        case "item":
                            noOfItemInChannel++;
                            XmlNodeList xmlChildNodeOfItem = channelList[k].ChildNodes[i].ChildNodes;
                            //debugging
                            //Console.WriteLine("Thread Name in item in RssFileReader" + Thread.CurrentThread.Name);

                            rssDocumentObject.ListOfChannel[k].ListOfItem.Add(new Item());

                            for (int j = 0; j < xmlChildNodeOfItem.Count; j++)
                            {
                                switch (xmlChildNodeOfItem[j].Name)
                                {
                                    case "title":
                                        rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemTitle.InnerText = xmlChildNodeOfItem[j].InnerText;
                                        break;
                                    case "link":
                                        rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemLink.InnerText = xmlChildNodeOfItem[j].InnerText;
                                        break;
                                    case "description":
                                        rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemDescription.InnerText = xmlChildNodeOfItem[j].InnerText;
                                        break;
                                    case "pubDate":
                                        try
                                        {
                                            string dateTimeTemp = xmlChildNodeOfItem[j].InnerText;
                                            char[] splitCharArray = new char[1];
                                            splitCharArray[0] = ' ';
                                            string[] splitedDateTimeTemp = dateTimeTemp.Split(splitCharArray);
                                            string RFC822 = "ddd,ddMMMyyyyHH:mm:ss";
                                            rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemPubDate.PublicationDate = DateTime.ParseExact(splitedDateTimeTemp[0] + splitedDateTimeTemp[1] + splitedDateTimeTemp[2] + splitedDateTimeTemp[3] + splitedDateTimeTemp[4], RFC822, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
                                        }
                                        //exception not rethrown default date is assigned 
                                        catch (Exception e)
                                        {
                                            //Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);
                                        }
                                        break;
                                    case "guid":
                                        rssDocumentObject.ListOfChannel[k].ListOfItem[noOfItemInChannel].ItemGuid.InnerText = xmlChildNodeOfItem[j].InnerText;
                                        break;


                                }
                            }
                            break;
                        case "title":
                            rssDocumentObject.ListOfChannel[k].ChannelTitle.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "description":
                            rssDocumentObject.ListOfChannel[k].ChannelDescription.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "link":
                            rssDocumentObject.ListOfChannel[k].ChannelLink.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;

                        case "language":
                            rssDocumentObject.ListOfChannel[k].ChannelLanguage.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "pubDate":
                            try
                            {
                                string dateTimeTempForChannel = channelList[k].ChildNodes[i].InnerText;
                                char[] splitCharArrayForChannel = new char[1];
                                splitCharArrayForChannel[0] = ' ';
                                string[] splitedDateTimeTempForChannel = dateTimeTempForChannel.Split(splitCharArrayForChannel);
                                string formatStringForChannel = "ddd,ddMMMyyyyHH:mm:ss";
                                rssDocumentObject.ListOfChannel[k].ChannelPubDate.PublicationDate = DateTime.ParseExact(splitedDateTimeTempForChannel[0] + splitedDateTimeTempForChannel[1] + splitedDateTimeTempForChannel[2] + splitedDateTimeTempForChannel[3] + splitedDateTimeTempForChannel[4], formatStringForChannel, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
                            }
                            //exception not rethrown default date is assigned 
                            catch (Exception e)
                            {
                                //Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);

                            }
                            break;
                        case "lastBuildDate":
                            try
                            {
                                string dateTimeTempForChannel = channelList[k].ChildNodes[i].InnerText;
                                char[] splitCharArrayForChannel = new char[1];
                                splitCharArrayForChannel[0] = ' ';
                                string formatStringForChannel = "ddd,ddMMMyyyyHH:mm:ss";

                                string[] splitedDateTimeTempForChannel = dateTimeTempForChannel.Split(splitCharArrayForChannel);
                                rssDocumentObject.ListOfChannel[k].ChannelLastBuildDate.LastBldDate = DateTime.ParseExact(splitedDateTimeTempForChannel[0] + splitedDateTimeTempForChannel[1] + splitedDateTimeTempForChannel[2] + splitedDateTimeTempForChannel[3] + splitedDateTimeTempForChannel[4], formatStringForChannel, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
                            }
                            //exception not rethrown default date is assigned 
                            catch (Exception e)
                            {
                                //Console.WriteLine("Exception while formatting string to datetime in rssFileReader():" + e);

                            }

                            break;

                        case "docs":
                            rssDocumentObject.ListOfChannel[k].ChannelDocs.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "generator":
                            rssDocumentObject.ListOfChannel[k].ChannelGenerator.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "managingEditor":
                            rssDocumentObject.ListOfChannel[k].ChannelManagingEditor.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "webMaster":
                            rssDocumentObject.ListOfChannel[k].ChannelWebMaster.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;
                        case "ttl":
                            rssDocumentObject.ListOfChannel[k].ChannelTtl.InnerText = channelList[k].ChildNodes[i].InnerText;
                            break;

                    }
                }


            }
        }
        catch(Exception e)
        {
            e.Source = "RssFileReader:Reading xml document object data to rss object";
            throw;
        }
        Console.WriteLine("    Thread out RssFilereader :" + Thread.CurrentThread.Name);
        return rssDocumentObject;
    }
}


note:-while debugging i got to know that its taking time while loading xml document. is there any limit imposed on system for number of webrequest objects?

+3  A: 

If you're using web requests to the same server, those are throttled by default to two connections at a time.

You're then failing to dispose of the response and response stream, which means it'll wait until it's garbage collected to release the connection for use elsewhere. Change your loading code to:

    try
    {
        // Removed unnecessary @ signs
        using (WebResponse response = HttpClient.GetWebResponse(
                  rssFeed, null, null, 1200000, "http://www.sahil.com"))
        using (Stream responseStream = response.GetResponseStream())
        {
            documentObj.Load(responseStream);
        }
    }
    catch(Exception e)
    {
        e.Source = "RssFileReader:Loading xmldocument object";
        throw;
    }

It's possible that you only need to close the web response - that the stream will be taken care of by closing the response - but it's better to be sure.

You'll still only get two threads using connections to the same server at the same time (by default, for non-ASP.NET; you can change this yourself using ServicePointManager.DefaultConnectionLimit or the connectionManagement config file element) but at least then you won't need to wait while the connection idles out or waits for garbage collection.

Jon Skeet
Downvoters: please give reasons.
Jon Skeet
A: 

it was taking time when i was running it from console applcation fr testing. reason fo much time taken was io blocking because of pint statements fr debugging.

Maddy.Shik