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

  • SEARCH
  • Home
  • 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 4620874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:37:10+00:00 2026-05-22T02:37:10+00:00

At the moment I have a very strange issue regarding locking a dictionary. I

  • 0

At the moment I have a very strange issue regarding locking a dictionary. I have two different threads that access the dictionary, one thread puts in some entries, the other tries to read them out. All is well while making the entries, the strangest of problems occurs when I try to extract some values from the dictionary.

Here is what it looks like

 lock (myDictionary)
{
 //Add an entry here for a key.
}

// Retreival

lock(myDictionary)
{

 if (myDictionary.ContainsKey(key))
 myDictionary.TryGetValue(key, out store);
}

As soon as a thread enters the above code, the keys are not found. BUT if I write the following

lock(myDictionary)
{
  Console.WriteLine(myDicionary.Count)
 if (myDictionary.ContainsKey(key))
   myDictionary.TryGetValue(key, out store);
}

Out of nowhere I see the same dictionary containing the keys that were not found in the prior code. Now you guys can argue that what happened was the I/O delay caused enough time for the dictionary to store some values, but I donot have any timeouts or something that causes me to loose any processing, what this means till the dictionary contains some values to be extracted, next processing step will not occur and my program will wait till it gets some values. But unfortunately, this does not happen unless I put a Console.WriteLine… Anyone faced such situation before?

  • 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-22T02:37:11+00:00Added an answer on May 22, 2026 at 2:37 am

    The problem is most likely in the code that you are not showing. The type of issues that you describe is very common in multi-threading scenarios: everything comes down to timing, and timing is different when you change your code ever so slightly. In your particular case whatever value you are trying to access is there or not there depending on timing of what happening in other threads. It is simply possible that the value was not put there yet or was already deleted, when you code can’t find it, but is present when you code can’t see it.

    It is impossible to say for sure, without seeing the rest of your code, especially the part that writes / deletes to / from the collection.

    Few other things:

    • you need to lock both read and write access
    • even if you lock both read and write access you can’t guaranty that a certain object is there unless you also use some other type of synchronization
    • As other said using ContainsKey in combination with TryGetValue does not make much sense, the way you wrote it. ‘TryGetValue’ exists so that you do NOT need to call ContainsKey: if value is there it will be returned to you straight away and if it’s not, the result of the method call will tell you this.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange issue on my hands. I have two IIS websites
I have a very painful library which, at the moment, is accepting a C#
I have a list view control which at the moment only allows one item
I have a Java client that calls a web service at the moment using
For the moment the best way that I have found to be able to
I'll give a very simple example: at the moment I have to write like
At the moment I have setup a custom ok cancel dialog with a drop
At the moment I have a set of divs, generated dynamically by php and
Working on a project at the moment and we have to implement soft deletion
I have a self built JSP webapp and at the moment I'm using tomcats

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.