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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:41:25+00:00 2026-05-25T16:41:25+00:00

We call a (thread-safe) static method in a multi-threaded context, but need to add

  • 0

We call a (thread-safe) static method in a multi-threaded context, but need to add returned error messages to some sort of list.

In .NET 4, a ConcurrentDictionary could be used. But what is an alternative for the allMessages object below if I need to run this in 3.5?

 using (var mre = new ManualResetEvent(false))
 {
     for (int i = 0; i < max1; i++)
     {
          ThreadPool.QueueUserWorkItem(st =>
          {
            for (int j = 0; j < max2; j++)
            {
                string errorMsg;
                // Call thread-safe static method
                SomeStaticClass.SomeStaticMethod(out errorMsg);

                // Need to collect all errorMsg in some kind of threadsafe list
                // allMessages.Add(errorMsg);

             }
             if (Interlocked.Decrement(ref pending) == 0) mre.Set();
           });
    }
    mre.WaitOne();
 }

The allMessages-object can be as simple as possible – any kind of list would do that is capable of being filled with strings in a thread-safe manner.

  • 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-25T16:41:26+00:00Added an answer on May 25, 2026 at 4:41 pm

    You could write your own implementation of the ICollection<T> interface, where you just delegate all calls to an private member of type List<T>, protecting every access with lock.

    E.g.

    public class LockedCollection<T> : ICollection<T>
    {
       private readonly object _lock = new object();
       private readonly List<T> _list = new List<T>();
    
       public void Add(T item)
       {
          lock (_lock)
          {
             _list.Add(item);
          }
       }
    
       // other members ...
    
    }
    

    If that is worth the “effort” (and code to maintain), is the question you need to decide. You could just protect your calls to a “plain” List with a lock/monitor.

    E.g.

                for (int j = 0; j < max2; j++)
                {
                    string errorMsg;
                    // Call thread-safe static method
                    SomeStaticClass.SomeStaticMethod(out errorMsg);
    
                    lock (allMessageLock) // allMessagesLock is an "object", defined with the same lifetime (static- or instance-member) as "allMessages".
                    {
                       // Need to collect all errorMsg in some kind of threadsafe list
                       allMessages.Add(errorMsg);
                    }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I'm trying to call Dispatcher.PushFrame() from several different thread but encounter an error: Must
Instead of writing the following non-thread safe method. private static final Calendar calendar =
Is the standard Java 1.6 javax.xml.parsers.DocumentBuilder class thread safe? Is it safe to call
I create a second thread to call a method that downloads several images using:
How would I call a method with the below header on a thread? public
I have a multi-threaded application which parses some text and it needs to use
For being thread safe do we need to assign function parameters to the local
I need to use random class to generate random numbers in a multi threaded
The context objects generated by Entity Framework are not thread-safe. What if I use

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.