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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:32:01+00:00 2026-06-14T15:32:01+00:00

.NET Framework 3.5 I have two threads that using the same generic collection. One

  • 0

.NET Framework 3.5

I have two threads that using the same generic collection. One thread loop through the collection using the foreach statement:

while(HaveToContinue)
{
   // Do work 1

   try
   {
      foreach(var item in myDictionary)
      {
         // Do something with/to item
      }

      // Do work 2 (I need to complete the foreach first)
   }
   catch(InvalidOperationException)
   {
   }
}

At the same time the other thread modify the collection:

// The following line causes the InvalidOperationException (in the foreach)
myDictionary.Remove(...);

So, is there a way to avoid this InvalidOperationException? If I could avoid this exception I could complete my work (work 1 + work 2) all times, instead, every time I catch the exception, I can’t finish the work.

I thought to use a ManualResetEvent object, something like this:

while(HaveToContinue)
{
   // Do work 1

   try
   {
      myResetEvent.Reset();
      foreach(var item in myDictionary)
      {
         // Do something with/to item
      }
      myResetEvent.Set();

      // Do work 2 (I need to complete the foreach first)
   }
   catch(InvalidOperationException)
   {
   }
}

And every time the other thread modify the collection:

// Expect the foreach is completed
myResetEvent.WaitOne();
// And then modify the collection
myDictionary.Remove(...);

But probably there is a better solution.

  • 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-06-14T15:32:02+00:00Added an answer on June 14, 2026 at 3:32 pm

    If you’re using .NET 4, you should use the ConcurrentBag or ConcurrentDictionary class instead, which are thread-safe.

    If you’re using an earlier version, then the easiest (albeit inefficient) solution would be to use a lock. You could instantiate this as a plain object:

    private readonly object sync = new object();
    

    Then, when you need to access the list, acquire the lock first:

    while (HaveToContinue)
    {
       // Do work 1
    
       lock (sync)
       {
          foreach (var item in myDictionary)
          {
             // Do something with/to item
          }
       }
    
       // Do work 2 (I need to complete the foreach first)
    }
    

    Similarly when modifying the collection, acquiring the same lock:

    lock (sync)
    {
        myDictionary.Remove(...);
    }
    

    If the amount of work you have to do on each item is substantial, then it would be more efficient to first obtain a local copy of the dictionary, release the lock, and then proceed to iterate over the said copy, allowing the racing thread to modify the global dictionary:

    while (HaveToContinue)
    {
       // Do work 1
    
       Dictionary<Key,Value> localDictionary;
    
       lock (sync)
       {
          localDictionary = new Dictionary<Key,Value>(myDictionary);
       }
    
       foreach (var item in localDictionary)
       {
          // Do something with/to item
       }
    
       // Do work 2 (I need to complete the foreach first)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two websites, both using .Net framework 3.5. One website is hosting a
I'm using ASP.NET MVC 3 with Entity Framework CodeFirst I have two classes as
I have two indexed (8-bits) Bitamp, both using the same Palette in C# (.NET
I have an application that uses the .NET framework 3.5. I am building this
I have a asp.net web site that was developed on the .Net Framework v2
What I have created a very simple asp.net web service using .NET framework 3.5,
I have a .NET Compact Framework app that can runs on three windows machines
I am just starting out with ADO.net Entity Framework I have mapped two tables
I have developed a .net application using .net framework 4.0 where I have used
I have a small .NET application that requires the .NET Framework 4 Client Profile.

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.