Dot net: threads running too slow in visual edition visual studio 2008?
I am running 12 threads. The function that these threads call has no blocking on any object. but still these threads take too long. (16 minutes). Every thread parses an XML document which works fine if run indiviually.is this problem for some reason related to the max flow provided by express release or some dn blocking with express release.
function code, each called threads. each thread is given a 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 found out that it takes time when loading the xml document. is there a system limit for the number of webrequest objects?
a source to share
If you are using web requests to the same server, they are throttled by two connections by default.
Then you don't delete the reply and reply stream, which means that it waits until garbage collected to free up the connection for use elsewhere. Change your upload 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;
}
Perhaps you only need to close the web response - that the thread will be taken care of by closing the response, but it's better to be sure.
You will still only receive two streams using connections to the same server (default for nonASP.NET, you can change this yourself using the ServicePointManager.DefaultConnectionLimit
or connectionManagement
config file ), but at least you don't need to wait for the connection idle or waiting for garbage collection.
a source to share