concurrent-collections

What to add for the update portion in ConcurrentDictionary AddOrUpdate

余生颓废 提交于 2019-11-30 10:42:35
问题 I am trying to re-write some code using Dictionary to use ConcurrentDictionary. I have reviewed some examples but I am still having trouble implementing the AddOrUpdate function. This is the original code: dynamic a = HttpContext; Dictionary<int, string> userDic = this.HttpContext.Application["UserSessionList"] as Dictionary<int, String>; if (userDic != null) { if (useDic.ContainsKey(authUser.UserId)) { userDic.Remove(authUser.UserId); } } else { userDic = new Dictionary<int,string>(); }

What to add for the update portion in ConcurrentDictionary AddOrUpdate

試著忘記壹切 提交于 2019-11-29 21:59:06
I am trying to re-write some code using Dictionary to use ConcurrentDictionary. I have reviewed some examples but I am still having trouble implementing the AddOrUpdate function. This is the original code: dynamic a = HttpContext; Dictionary<int, string> userDic = this.HttpContext.Application["UserSessionList"] as Dictionary<int, String>; if (userDic != null) { if (useDic.ContainsKey(authUser.UserId)) { userDic.Remove(authUser.UserId); } } else { userDic = new Dictionary<int,string>(); } userDic.Add(authUser.UserId, a.Session.SessionID.ToString()); this.HttpContext.Application["UserDic"] =

Why is ConcurrentBag<T> so slow in .Net (4.0)? Am I doing it wrong?

天大地大妈咪最大 提交于 2019-11-28 04:51:51
Before I started a project, I wrote a simple test to compare the performance of ConcurrentBag from (System.Collections.Concurrent) relative to locking & lists. I am extremely surprised that ConcurrentBag is over 10 times slower than locking with a simple List. From what I understand, the ConcurrentBag works best when the reader and writer is the same thread. However, I hadn't thought it's performance would be so much worse than traditional locks. I have run a test with two Parallel for loops writing to and reading from a list/bag. However, the write by itself shows a huge difference: private

How to work threading with ConcurrentQueue<T>

妖精的绣舞 提交于 2019-11-27 13:14:40
I am trying to figure out what the best way of working with a queue will be. I have a process that returns a DataTable. Each DataTable, in turn, is merged with the previous DataTable. There is one problem, too many records to hold until the final BulkCopy (OutOfMemory). So, I have determined that I should process each incoming DataTable immediately. Thinking about the ConcurrentQueue<T> ...but I don't see how the WriteQueuedData() method would know to dequeue a table and write it to the database. For instance: public class TableTransporter { private ConcurrentQueue<DataTable> tableQueue = new

What is the difference between SynchronizedCollection<T> and the other concurrent collections?

末鹿安然 提交于 2019-11-27 12:17:17
How does SynchronizedCollection<T> and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection<T> being a class? SynchronizedCollection<T> and all of the classes in Concurrent Collections provide thread-safe collections. How do I decide when to use one over the other, and why? The SynchronizedCollection<T> class was introduced first in .NET 2.0 to provide a thread-safe collection class. It does this via locking so that you essentially have a List<T> where every access is

How to work threading with ConcurrentQueue<T>

给你一囗甜甜゛ 提交于 2019-11-26 16:07:59
问题 I am trying to figure out what the best way of working with a queue will be. I have a process that returns a DataTable. Each DataTable, in turn, is merged with the previous DataTable. There is one problem, too many records to hold until the final BulkCopy (OutOfMemory). So, I have determined that I should process each incoming DataTable immediately. Thinking about the ConcurrentQueue<T> ...but I don't see how the WriteQueuedData() method would know to dequeue a table and write it to the

What is the difference between SynchronizedCollection<T> and the other concurrent collections?

元气小坏坏 提交于 2019-11-26 15:57:31
问题 How does SynchronizedCollection<T> and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection<T> being a class? SynchronizedCollection<T> and all of the classes in Concurrent Collections provide thread-safe collections. How do I decide when to use one over the other, and why? 回答1: The SynchronizedCollection<T> class was introduced first in .NET 2.0 to provide a thread-safe