Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:42:58+00:00 2026-05-13T22:42:58+00:00

I have a static dictionary which holds authentications for a web service. I’m running

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:42:58+00:00Added an answer on May 13, 2026 at 10:42 pm

    I would suggest working out everything you want to remove first, and then doing it all in one lock:

    var itemsToRemove = expired.Select(e => e.Value.Token).ToList();
    
    using (new WriteLock(dictionaryLock))
    {
        foreach (var removal in itemsToRemove)
        {
            _authentications.Remove(removal);
        }
    }
    

    Aside from anything else, this means you won’t be trying to remove an item from the dictionary while iterating over it.

    (Just to check: is the key to each entry the same as the Token property value? If so, you can just use e.Key rather than e.Value.Token – that may be somewhat clearer.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, which holds a static dictionary of all existing instances, which
I have static method which plays music. How to return value from this method
I have static method which returns me as it's name says data from domain
I have a dictionary which is filled once in a static constructor and stays
I have this CacheManager class which keeps a static dictionary with all sorts of
in my Class I have a static dictionary of strings object which contains a
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I have several service classes that have static methods and offer a service to
Why can we have static final members but cant have static method in an
I intended to create a class which only have static members and static functions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.