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

Currently, I have a static factory method like this: public static Book Create(BookCode code)
In project A, i have a static class which stores data for specific types,
I have a dictionary of 10000 Product/Colour/Size combinations which I have created with something
TL;DR: Which is likely faster: accessing static local variable, accessing variable stored in HttpRuntime.Cache,
I have the following code which lets me execute a workflow. This could be
I have an overloaded generic method used to obtain the value of a property
I have a static website with files like index.php, blog.php, contact.php etc How can
I have one static class with two property. public class SiteDetails { public static
I have a project in NetBeans where two different classes have public static void
I have a BroadcastReceiver class. I have some static variables declared whose value is

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.