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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:59:11+00:00 2026-05-15T23:59:11+00:00

During adding key to dictionary, it is crashing if I don’t lock on it

  • 0

During adding key to dictionary, it is crashing if I don’t lock on it and giving NullReferenceException which is reasonable

Rarely sometimes it is also crashing when I am adding element to the Dictionary Value (list reference), which is weird…

I have another question as well. The files are in text format. Sometimes reading them takes 1890 ms, and other times it is taking 10 times as much as that. The runs are made consecutive. Is there a possiblity that something is busying the I/O buffer suddenly

Any recommendation to at least stablize this…

        private static void ParallelReadAndCalculate(FileInfo[] files)
    {
        Stopwatch sw1 = Stopwatch.StartNew();
        while (!Parallel.ForEach(files, (FileInfo file) => ReadFileToEnd(file)).IsCompleted) ;
        Console.WriteLine(sw1.ElapsedMilliseconds);
    }

        private static void ReadFileToEnd(FileInfo file)
    {
        string key = file.Name.Split('.')[0];
        lock (ListOfCompanyData)
            if (!ListOfCompanyData.ContainsKey(key))
            {
                ListOfCompanyData.Add(key, new List<string>(19800));
            }
        string line = "";

        using (StreamReader streamReader = (new StreamReader(file.FullName)))
        {                
            while ((line = streamReader.ReadLine()) != null) {
              // this is giving KeyNotFoundException sometimes and others, do I need to lock here given the fact that I am accessing different references synchronously
                    ListOfCompanyData[key].Add(line);                
            }                                                       
        }

    }
  • 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-15T23:59:12+00:00Added an answer on May 15, 2026 at 11:59 pm

    I would move the adding to the dictionary outside the parallel loop. This saves you a lock (at the cost of an extra loop), and makes it at least more practical to inspect the contents of the dictionary before the parallel processing.

    Besides, it’s the reading that you want done in parallel, not the adding to the dictionary.

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

Sidebar

Related Questions

Sometimes, during period of high traffic on sites, CCK input fields disappear while adding
During my apprenticeship, I have used NHibernate for some smaller projects which I mostly
During software development, there may be bugs in the codebase which are known issues.
During a discussion about security, a developer on my team asked if there was
During a typical day programming, I implement functions in a way that I would
During a complicated update I might prefer to display all the changes at once.
During an ASP.NET page load I'm opening and closing multiple System.Data.SqlClient.SqlConnections inside multiple controls
During the installation of Apache2 I got the following message into cmd window: Installing
During my work with databases I noticed that I write query strings and in
During execution, how can a java program tell how much memory it is using?

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.