2014-02-17 20 views
0

什麼是加載來自Web服務中各種源的異步結果的最快和優化的方式?加載異步結果的最快和優化的方式

Dictionary<string, int> readySources = GetReadyHotelSources(member); 

     eventFlag = new AutoResetEvent[readySources.Count]; 
     int maxTimeOut = 0; 
     int i = 0; 
     foreach (KeyValuePair<string, int> activeSource in readySources) 
     { 
      maxTimeOut = (int)activeSource.Value > maxTimeOut ? (int)activeSource.Value : maxTimeOut; 
      i++; 
     } 
     int j = 0;    
     foreach (KeyValuePair<string, WaitCallback> deThread in listOfThreads) 
     { 
      if (readySources.ContainsKey(deThread.Key)) 
      { 
       ThreadPool.QueueUserWorkItem(deThread.Value, j); 
       eventFlag[j] = new AutoResetEvent(false); 
       j++;      
      } 
     } 

     if (j != 0) 
     { 
      if (!WaitHandle.WaitAll(eventFlag, new TimeSpan(0, 0, maxTimeOut), true)) 
      { 
       //TODO: audit which thread is timed out     
      } 
     }   
     // combined sources  
     result = CombineHotelSources(); 

現在我想在異步模式下在Web服務中移動此代碼。

+0

如果這些不同的數據源是CPU限制的計算任務,那麼在服務器端並行執行它們沒有意義。這可能會極大地影響可伸縮性。如果這些源是IO /網絡綁定,這可能有所幫助:http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4 – Noseratio

+0

是的,這些是我們正在打網絡的外部資源。 – Rohit

回答