Hi
I have a static dictionary which holds authentications for a web service. I'm running a timer (hence the TimerCallBack sig on the method below) to remove those authentications which have been dormant for a period.
I'm not quite sure of the locking behaviour I need in relation to linq. Here's my code thus. I'm slightly concerned that I may affect performance by using too many locks. e.g. Would it be better to just take a single writelock and no readlocks (even if no authentications had expired)?
private static void RemoveExpiredAuthentications(object o)
{
var expired = from a in _authentications
where a.Value.LastAccessed.AddSeconds(expired_interval_secs) < DateTime.Now
select a;
using (new ReadLock(dictionaryLock)) {
expired.Select(e => {
using (new WriteLock(dictionaryLock)){
_authentications.Remove(e.Value.Token);
}
}
}
}
Many thanks
Simon