2014-04-08 27 views
0

我想在解析一個大的XML文件後創建一個嵌套的ConcurrentDictionary。 運行下面的代碼時得到的是最內層(tmpDictionaryDid)字典爲空。嵌套ConcurrentDictionary

我想我沒有妥善保存字典或加載不正確,但我不明白什麼是不正確的。

它甚至有可能用ConcurrentDictionary解決它,或者我應該嘗試使用另一種解決方案嗎?

public void temp22db() 
    { 
     ConcurrentDictionary<string, ConcurrentDictionary<string, string>> dictionary22 = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>(); 
     try 
     { 
      ConcurrentDictionary<string, string> did = new ConcurrentDictionary<string, string>(); 

      did.TryAdd("F186 1", "test1"); 
      did.TryAdd("F186 2", "test2"); 
      dictionary22.TryAdd("F186", did); 

      did.TryAdd("EDA0 1", "test3"); 
      did.TryAdd("EDA0 2", "test4"); 
      dictionary22.TryAdd("EDA0", did); 

      string test; 
      did.TryGetValue("EDA0 1", out test); 
      Helper.logInWindow("This works " + test); 
     } 
     catch (Exception e) 
     { 
      Helper.logInWindow("SDDBTemp: " + e.Message); 
     } 
     ecuDictionary2.TryAdd("1638", dictionary22); 
    } 

public string getDIDInterpretation() 
    { 
     string outStr = ""; 
     ConcurrentDictionary<string, ConcurrentDictionary<string, string>> tmpDictionary; 
     ecuDictionary2.TryGetValue("1638", out tmpDictionary); 

     if (tmpDictionary != null) 
     { 
      ConcurrentDictionary<string, string> tmpDictionaryDid; 
      tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid); 

      if (tmpDictionaryDid != null) 
      { 
       tmpDictionaryDid.TryGetValue("EDA0 1", out outStr); 
      } 
     } 

     Helper.logInWindow("Why U no work? " + outStr); 
     return outStr; 
    } 
+1

你應該儘量避免嵌套concurrentdictionaries - 你會碰到更多的併發與此相關的問題。嘗試將嵌套關鍵字組合成只有一個關鍵字,例如代替字典[key1] [subkey1],將其存儲爲字典[key1 + subkey1] – jaminto

回答

0

您應該使用它像這樣來代替:(父ConcurrentDictionary將允許併發到你的內部字典)

public static void temp22db() 
    { 
     Dictionary<string, Dictionary<string, string>> dictionary22 = new Dictionary<string, Dictionary<string, string>>(); 
     try 
     { 
      Dictionary<string, string> did = new Dictionary<string, string>(); 

      did.Add("F186 1", "test1"); 
      did.Add("F186 2", "test2"); 
      dictionary22.Add("F186", did); 

      did.Add("EDA0 1", "test3"); 
      did.Add("EDA0 2", "test4"); 
      dictionary22.Add("EDA0", did); 

      string test; 
      did.TryGetValue("EDA0 1", out test); 
      Helper.logInWindow("This works " + test); 
     } 
     catch (Exception e) 
     { 
      Helper.logInWindow("SDDBTemp: " + e.Message); 
     } 
     ecuDictionary2.TryAdd("1638", dictionary22); 
    } 

    public static string getDIDInterpretation() 
    { 
     string outStr = ""; 
     Dictionary<string, Dictionary<string, string>> tmpDictionary; 
     ecuDictionary2.TryGetValue("1638", out tmpDictionary); 

     if (tmpDictionary != null) 
     { 
      Dictionary<string, string> tmpDictionaryDid; 
      tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid); 

      if (tmpDictionaryDid != null) 
      { 
       tmpDictionaryDid.TryGetValue("EDA0 1", out outStr); 
      } 
     } 

     Helper.logInWindow("Why U no work? " + outStr); 
     return outStr; 
    }