concurrentdictionary

Is the list order of a ConcurrentDictionary guaranteed?

人盡茶涼 提交于 2019-12-20 03:41:15
问题 I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically be last. Is this because ConcurrentDictionary doesnt guarantee a persistent order on the IEnumerate interface, or what can be the reason? 回答1: No ConcurrentDictionary (and Dictionary<T> for that matter) does not guarantee the ordering of the keys

getting argument exception in concurrent dictionary when sorting and displaying as it is being updated

▼魔方 西西 提交于 2019-12-19 08:55:19
问题 I am getting a hard to reproduce error in the following program in which a number of threads update a concurrent dictionary in parallel and the main thread displays the state of the dictionary in sorted order after fixed time intervals, until all updating threads complete. public void Function(IEnumerable<ICharacterReader> characterReaders, IOutputter outputter) { ConcurrentDictionary<string, int> wordFrequencies = new ConcurrentDictionary<string, int>(); Thread t = new Thread(() =>

How to get moment-in-time snapshot of ConcurrentDictionary in C#?

谁说胖子不能爱 提交于 2019-12-14 03:54:23
问题 MSDN states that the enumerator returned from the dictionary does not represent a moment-in-time snapshot of the dictionary. Although it will be rarely needed in multithreaded environment, but if one wants, what is the best way to get the moment-in-time snapshot of ConcurrentDictionary? 回答1: Just call ToArray() method. Here is a source code: /// <summary> /// Copies the key and value pairs stored in the <see cref="ConcurrentDictionary{TKey,TValue}"/> to a /// new array. /// </summary> ///

Extension method Gets “No overload for method” Error

柔情痞子 提交于 2019-12-13 08:26:31
问题 I just recently upgraded this project from ASP.Net 3.5 to 4.0 so that I could use the concurrentDictionary instead of Dictionary because of the thread safe feature. To use it I created an extension using code found in help forums. It is all very close to working and I don't know how I can modify the extension for it to work properly. Here is the code: var catalogs = (from _catalog in entities.catalogs from rolePermission in entities.c_roleperm from _group in entities.c_group from _user in

How should I “Cancel” an AddOrUpdate within ConcurrentDictionary?

倖福魔咒の 提交于 2019-12-12 15:17:28
问题 I've read the MSDN documents and this blog and I need the following logic: For a ConcurrentDictionary<string,bool> If the string doesn't exist, add it,and make sure I set the bool to True while adding If the string does exist, only change the bool to True if it's false. Otherwise cancel the update My use case I have several DNS Domains to scan for malware. There is a good likelihood that there will be duplicates in the list that I retrieve in realtime. I receive the list of DNS Domains in

What could solves this multi-threaded scenario better than Concurrent collections

我与影子孤独终老i 提交于 2019-12-12 13:51:03
问题 I have a persistent B+tree, multiple threads are reading different chunks of the tree and performing some operations on read data. Interesting part: each thread produces a set of results, and as end user I want to see all the results in one place. What I do: one ConcurentDictionary and all threads are writing to it. Everything works smooth this way. But the application is time critical, one extra second means a total dissatisfaction. ConcurentDictionary because of the thread-safety overhead

Why can I not add null as a value when using ConcurrentDictionary?

余生长醉 提交于 2019-12-12 00:00:44
问题 Consider the following code: // holds the actual values private volatile ConcurrentDictionary<string, Object> values; public object this[string key] { get { // exception is thrown on this line return values.GetOrAdd(key, null); } set { values.AddOrUpdate(key, value, (k, v) => value); } } What I want to do is simply create the entry in the dictionary if it does not exist yet; it should have no value though until something explicitly sets it. I get this exception though: An unhandled exception

Whats the risk of using TPL with ConcurrentDictionary with “addValueFactory”? MSDN implies threading issues

吃可爱长大的小学妹 提交于 2019-12-10 18:50:30
问题 MSDN says the following about addValueFactory in a multi threaded environment: Remarks If you call AddOrUpdate simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the dictionary for every call. There is a closed connect issue on this, but nothing seems to have been done. When I read the above excerpt I guess that one of these things might be happening: The Update() method is called continuously until it succeeds

Different behaviour when collection modified between Dictionary and ConcurrentDictionary

风格不统一 提交于 2019-12-08 00:44:18
问题 With a normal Dictionary code as list below, I get exception that Collection was modified; enumeration operation may not execute. Dictionary<int, int> dict2 = new Dictionary<int, int>(); dict2.Add(1, 10); dict2.Add(2, 20); dict2.Add(3, 30); dict2.Add(4, 40); foreach (var d in dict2) { if (dict2.ContainsKey(2)) dict2.Remove(2); if (dict2.ContainsKey(3)) dict2.Remove(3); } However with ConcurrentDictionary, this works fine. ConcurrentDictionary<int, int> dict1 = new ConcurrentDictionary<int,

Different behaviour when collection modified between Dictionary and ConcurrentDictionary

☆樱花仙子☆ 提交于 2019-12-06 05:06:32
With a normal Dictionary code as list below, I get exception that Collection was modified; enumeration operation may not execute. Dictionary<int, int> dict2 = new Dictionary<int, int>(); dict2.Add(1, 10); dict2.Add(2, 20); dict2.Add(3, 30); dict2.Add(4, 40); foreach (var d in dict2) { if (dict2.ContainsKey(2)) dict2.Remove(2); if (dict2.ContainsKey(3)) dict2.Remove(3); } However with ConcurrentDictionary, this works fine. ConcurrentDictionary<int, int> dict1 = new ConcurrentDictionary<int, int>(); dict1.AddOrUpdate(1, 10, (k,v)=> 10); dict1.AddOrUpdate(2, 20, (k, v) => 20); dict1.AddOrUpdate(3