2009-05-19 5 views
1

J'exécute 12 threads. La fonction que ces threads appellent n'a aucun verrou sur un objet. Cependant, ces threads prennent trop de temps. (16 minutes). Chaque thread analyse un document xml qui fonctionne bien si run indiviually.is ce problème à cause d'une raison liée max aucun threads fournis par l'édition expresse ou un blocage dn par l'édition expresse.dot net: des threads trop lents en édition express visual studio 2008?


code de fonction auquel chaque appel de thread est donné ci-dessous. chaque fil est donné différent 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: mise au point -Alors je suis arrivé à savoir que la prise du temps lors du chargement de document XML. Y a-t-il une limite imposée au système pour le nombre d'objets Webrequest?

+1

Avez-vous essayé de localiser le col de la bouteille? Essayez de lire des fichiers et non du réseau, pour voir si la carte réseau peut causer le goulot d'étranglement. Je ne pense pas qu'il y ait une limitation dans le threading dans Express, car le code est compilé au même temps d'exécution. – Thies

Répondre

0

cela prenait du temps lorsque je l'exécutais à partir de la console applcation fr testing. raison pour laquelle beaucoup de temps a été bloqué io en raison de déclarations de pinte fr débogage.

3

Si vous utilisez des requêtes Web sur le même serveur, celles-ci sont limitées par défaut à deux connexions à la fois.

Vous ne parvenez pas à éliminer le flux de réponse et de réponse, ce qui signifie qu'il attendra jusqu'à ce qu'il soit collecté pour libérer la connexion pour une utilisation ailleurs. Modifiez votre code de chargement:

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; 
    } 

Il est possible que vous ne devez fermer la réponse Web - que le flux sera pris en charge en fermant la réponse - mais il vaut mieux être sûr.

Vous ne recevrez toujours que deux threads en utilisant les connexions au même serveur en même temps (par défaut, pour nonASP.NET, vous pouvez le changer vous-même en utilisant ServicePointManager.DefaultConnectionLimit ou connectionManagement config file element) mais au moins vous avez gagné Il n'est pas nécessaire d'attendre que la connexion s'arrête ou attende la récupération de place.

+0

Downvoters: veuillez donner des raisons. –