2013-11-27 16 views
10

Ich habe ein Problem in parallel.foreach was "Index war außerhalb der Grenzen des Arrays". Ich füge einen Code für parallel.foreach und wo es abstürzt.Parallel.Foreach geben Fehler "Index war außerhalb der Grenzen des Arrays"

var lstFRItems = session.CreateCriteria<TFRItem>().Add(Restrictions.Eq("TSCEnterprise.FEnterpriseID", EnterpriseId)).AddOrder(Order.Asc("FName")).List<TFRItem>(); 
        List<FRItemAccount> lstItemAccount = new List<FRItemAccount>(); 
        var ListAccounts = session.CreateCriteria<TFRItemAccount>().List<TFRItemAccount>(); //lstFRItems.Select(i => new { i.TFRItemAccounts }).ToList(); 
        //foreach (var item in lstFRItems) 
       Parallel.ForEach(lstFRItems, item => 
       { 
        var lstItemAcc = ListAccounts.Where(i => i.TFRItem == item); //item.TFRItemAccounts.ToList(); 
        FRItemAccount account = new FRItemAccount(); 
        account.ItemID = item.FItemID; 
        account.ItemAccount = new List<ItemAccount>(); 
        // foreach (var itemAcct in lstItemAcc) 
        Parallel.ForEach(lstItemAcc, itemAcct => 
        { 
         ItemAccount oItemAccount = new ItemAccount(); 
         if (itemAcct != null) 
         { 
          oItemAccount.ItemAccountID = itemAcct.FItemAccountID; 

          if (itemAcct.TSCProperty == null) 
          { 
           oItemAccount.ForID = itemAcct.TSCCompany.FCompanyID; 
           oItemAccount.ForCompanyName = "Co# " + "- " + itemAcct.TSCCompany.FID + " " + itemAcct.TSCCompany.FName; 
           oItemAccount.FID = itemAcct.TSCCompany.FID; 
           oItemAccount.ForType = 1; 
          } 
          else 
          { 
           oItemAccount.ForID = itemAcct.TSCProperty.FPropertyID; 
           oItemAccount.ForCompanyName = "Prop# " + "- " + itemAcct.TSCProperty.FID + " " + itemAcct.TSCProperty.FName; 
           oItemAccount.FID = itemAcct.TSCProperty.FID; 
           oItemAccount.ForType = 2; 
          } 
          oItemAccount.Account = itemAcct.FAccount; 
          account.GLAccount = itemAcct.FAccount.ToString("#0.000"); //Formatted by Lhore Bansal 
          // account.Account = itemAcct.FAccount; 
          oItemAccount.isExisting = true; 
          //Original TFRItemAccount 
          oItemAccount.orgItemAccount = itemAcct; 
         } 
         if (lstItemAcc == null) 
          account.ItemID = item.FItemID; 
         account.ItemAccount.Add(oItemAccount); 
        }); 
        //Original tFRItem 
        account.Item = item; 
        //account.BaseAccount = Convert.ToDouble(item.FBaseAccount.ToString("F0")); // commented by jeet 
        account.BaseAccount = Convert.ToDouble((int)item.FBaseAccount); // added by jeet 
        account.Name = item.FName; 
        account.Type = item.FType; 
        lstItemAccount.Add(account); 
       }); 
        // tx.Commit(); 
        return Item = lstItemAccount; 

Es stürzt bei der drittletzten Zeile "lstItemAccount.Add (account)" ab. Als ich in lstItemAccount sah, hat es einige Zählungen und im Basisabschnitt hat es einen Fehler "base {System.SystemException} = {"Source array was not long enough. Check srcIndex and length, and the array's lower bounds."}".

Was ist die Lösung dieses Fehlers?

+0

fyi - Sie haben möglicherweise ein Threading-Problem mit 'LstItemAccount'. –

+0

Haben Sie eine funktionierende Antwort auf diese Frage erhalten? –

Antwort

8

Es hat mit

ListAccounts.Where(i => i.TFRItem == item); 
account.ItemAccount.Add(oItemAccount); 
lstItemAccount.Add(account); 

Listen und Arrays zu tun sind nicht Thread-sicher. Verwenden Sie ConcurrentBag statt

22

Ich würde eine ConcurrentBag<T> anstelle von List<T> verwenden. List<T> ist nur für den Zugriff eines Threads vorgesehen.

+0

Danke..Daniel.Es funktioniert jetzt gut. –

+0

Dies sollte als Antwort markiert werden. – garenyondem

Verwandte Themen