A line-up, user-wide system that for the first time in a few months has this system bug message:
"Items with the same index key have been added"
An item with the same key has already been added.
Analysis Reason:
C # Dictionary This kind of information structure of the change, if declared as static, in use, it is important to note that before writing or Add the information, only if ContainsKey is not enough (as shown in Figure 2), because it is not "execute;" Threads (thread) "Safe" (if more than one person writes at the same time, it is possible to cause an abnormal, even wrong.) The rate is not high, but a long time will happen once).
so before Dictionary write or ADD, you should do a lock action, as shown in Figure 3, so that only one person (one executive) can enter the lock block, and others will queue up.
Figure 1 Announces Dictionary changes
Figure 2 before the change (a long time will happen once the wrong way to write)
Figure 3 after the change
C # 's Lock,
But note that it is only effective if it is declared as a static change.
(Static variables, which represent all users of the entire site, share the same memory, not each user's own new memory).
-------------------------------------------------------------------
Dictionary (MSDN):
https://msdn.microsoft.com/zh-tw/library/xfhwa508 (v=vs.110). aspx
-------------------------------------------------------------------
A detailed article on the Dictionary Question (recommended):
Http://blog.darkthread.net/post-2012-01-31-dictionary-thread-safe.aspx
Above, mentioned in. NET 4.0 joins a new System.collections.concurrent.concurrentdictionary<tkey, Tvalue>
I don't see how we can solve this problem and recommend it.
-------------------------------------------------------------------
Lock Intro:
Http://www.dotblogs.com.tw/yc421206/archive/2011/01/07/20624.aspx
Https://msdn.microsoft.com/zh-tw/library/c5kehkcz.aspx
-------------------------------------------------------------------
Concurrentdictionary<tkey, tvalue> Category:
https://msdn.microsoft.com/zh-tw/library/dd287191 (v=vs.110). aspx
Concurrentdictionary<tkey, Tvalue> Getoradd method (TKey, TValue):
https://msdn.microsoft.com/zh-tw/library/ee378674 (v=vs.110). aspx
Dictionary + Locking versus Concurrentdictionary
Http://www.codeproject.com/Articles/548406/Dictionary-plus-Locking-versus-ConcurrentDictionar
-------------------------------------------------------------------
Concurrentdictionary makes your multithreaded code more beautiful
Http://www.bkjia.com/C_jc/968538.html
With dictionary, and when this property is promoted to static (class-level), we have to consider its thread safety because it can be accessed by multiple threads at the same time, and of course, if the object is read-only and thread-safe, but if this property can be written, Then we need to lock it up.
But from a performance perspective, we can not be received, we know that lock will lock the other lines outside, whether it is read or write, will be locked, performance is very poor.
-------------------------------------------------------------------
C # Dictionary use to pay attention to things