I'm using a dictionary to collect events in a multithread application, using lock when I add an event and not using it when I search for one. Every hour or so I run a cleanup of the events older than a certain time. Very simple and it works.
I'd like to move to a ConcurrentDictionary to remove the locks, and I thought I just had to add "Concurrent" and change the Add to TryAdd. But then I incurred in the error that LINQ returns only ToDictionary. I can obviusly not use LINQ, but I was curios to know if there's something I can do to preserve it. And more important, is there something I else I should consider before moving to ConcurrentDictionry?
public class messageResult
{
public Result result;
public DateTime receivedTime;
}
public Dictionary<Guid, messageResult> events = new Dictionary<Guid, messageResult>();
lock (events)
{
events = events.Where(p => p.Value.receivedTime >= t).ToDictionary(p => p.Key, p => p.Value);
}
Thanks