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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:02:11+00:00 2026-06-10T15:02:11+00:00

I have a mainthread and i do create another n-threads. Each of the n-threads

  • 0

I have a mainthread and i do create another n-threads. Each of the n-threads is populating a List<String>. When all threads are finished, they are joined, and i would like to have all those n-threads List in a List<List<String>> BUT in the mainthread. The mainthread should be able to operate on that List<List<String>>. Each of the n-threads contributed a List<String>.

I have c# .NET 3.5 and i would like to avoid a static List<List<String>>

            Thread t = new Thread(someObjectForThreading.goPopulateList);
            list_threads.Add(t);
            list_threads.last().start()

all those threads in list_threads go on and populate their List and when they are finished i would like to have something like

 //this = mainThread
 this.doSomethingWith(List<List<String>>)

Edit: Hmmm is there no a “standard concept” how to solve such a task? Many threads operating on a list and when all joined, the mainthread can proceed with operating on the list.

Edit2: the List<List<String>> listOfLists must not be static. It can be public or private. First i need the n-threads to operate (and lock) the listOfLists, insert their List and after all n-threads are done inserting their lists, i would join the threads and the mainthread could proceed with businesslogic and operate on the listOfLists

i think i will reRead some parts of http://www.albahari.com/threading/ report back

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

    Here’s a simple implementation using wait handles (in the case ManualResetEvent) to allow each worker thread to signal the main thread that it’s done with its work. I hope this is somewhat self explanatory:

    private List<List<string>> _listOfLists;
    
    public void CreateListOfLists()
    {
        var waitHandles = new List<WaitHandle>();
    
        foreach (int count in Enumerable.Range(1, 5))
        {
            var t = new Thread(CreateListOfStringWorker);
            var handle = new ManualResetEvent(false);
            t.Start(handle);
            waitHandles.Add(handle);
        }
    
        // wait for all threads to complete by calling Set()
        WaitHandle.WaitAll(waitHandles.ToArray());
    
        // do something with _listOfLists
        // ...
    }
    
    public void CreateListOfStringWorker(object state)
    {
        var list = new List<string>();
        lock (_listOfLists)
        {
            _listOfLists.Add(list);
        }
    
        list.Add("foo");
        list.Add("bar");
    
        ((ManualResetEvent) state).Set(); // i'm done
    }
    

    Note how I’m only locking while I add each thread’s List to the main list of lists. There is no need to lock the main list for each add, as each thread has its own List. Make sense?

    Edit:

    The point of using the waithandle is to wait for each thread to complete before working on your list of lists. If you don’t wait, then you run the risk of trying to enumerate one of the List instances while the worker is still adding strings to it. This will cause an InvalidOperationException to be thrown, and your thread(s) will die. You cannot enumerate a collection and simultaneously modify it.

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

Sidebar

Related Questions

I would like to create a thread passing a vector as parameter. but i
I have a class class with main thread public class MainThread extends Thread {
I have a windows form on the main thread and another thread that does
I currently have two threads running in my program: Main thread - Grabs image
I have an application that has two threads. The first one (the main thread)
I have a program that runs in a few threads. The main thread shares
I have a java application where the main-thread starts 2 other threads. If one
If I have two threads (Linux, NPTL), and I have one thread that is
I have been creating multiple background threads to parse xml files and recreate new
I have called a native program which creates another thread, which attaches itself to

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.