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 3981874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:27:45+00:00 2026-05-20T05:27:45+00:00

I need some help getting my head around thread safety and dictionaries. When adding

  • 0

I need some help getting my head around thread safety and dictionaries. When adding to a dictionary I get an error saying “An item with the same key has already been added” (when I say I get it, I haven’t actually been able to replicate this but have seen it in the error logs).

From what I can see when reading similar questions it’s likely to have something to do with thread safety, but I’m still not clear on what’s happening. And as it’s hard to test, especially when I can’t replicate it, I’m hoping that someone might be able to explain or point me in the right direction. It’s an asp.net web application (C#), and the error is happening when getting a quiz tailored for the specific user. I’m also getting errors trying to accessing keys that doesn’t exist, but one thing at a time!

I’ve made a mini example where I’ve stripped out everything I didn’t think was necessary to show the problem. If I’ve stripped out too much let me know.

public class QuizDataAdapterFactory
{
    private static IQuizDataAdapter q_adapter = new MyQuizDataAdapter();

    public static IQuizDataAdapter Create()
    {
        return q_adapter;
    }
}

IQuizDataAdapter dataAdapter = QuizDataAdapterFactory.Create();

public class MyQuizDataAdapter : IQuizDataAdapter
{
    private Quiz quiz;

    public Quiz GetQuiz()
    {
        quiz = new Quiz();
        quiz.QuestionIndex = new Dictionary<Guid, QuestionBase>();

        GetQuestions();

        return quiz;
    }

    private void GetQuestions()
    {
        Item[] items;

        foreach (Item questionItem in items)
        {
            Question newQuestion = new Question();
            PopulateQuestionFromItem(newQuestion, questionItem);
            questions.Add(newQuestion);

            // this is where it fails
            quiz.QuestionIndex.Add(questionItem.ID.ToGuid(), newQuestion);
        }
    }
}

Would adding [ThreadStatic] for the IQuizDataAdapter do the trick?

Thanks,

Annelie

  • 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-20T05:27:46+00:00Added an answer on May 20, 2026 at 5:27 am

    You’re only ever creating one instance of MyQuizDataAdapter due to your Create method being completely inappropriately named – it doesn’t “create” an adapter, it returns the same one every time!

    Create a different one each time instead, and you’ll at least be in a better position… admittedly I don’t much like the way that every time you call GetQuiz it repopulates the quiz in MyQuizDataAdapter too, but that’s another step…

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

Sidebar

Related Questions

I need help getting my head around the difference between my current OOP notion
I am getting a little confused and need some help please. Take these two
I am having trouble getting my head around this query for some reason and
I need some help. Here's what I'm getting right now: img ref I need
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help ... I'm a bit (read total) n00b when it comes
Hi I need some help with the following scenario in php. I have a

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.