title: multiple threads providing data to List<List<string>> to mainthread; multithreading
body: <p>I have multiple threads that create a <code>public List&lt;List&lt;String&gt;&gt; listOfLists</code> and fill it with data. After all threads are done, I would like to use <code>list.AddRange()</code> to combine all those lists from the multiple threads in the <code>listOfLits</code> of the main thread. </p>
  
  <p>So, if I have three threads and <code>listOfLists</code> of each thread has 10 elements, then <code>listOflists</code> of the main thread would have 30 elements. Just simple add 'em up and the mainthread object has to have all the data inserted by the threads.</p>
  
  <p>It works perfectly if I use the <code>static</code> keyword for the <code>listOfLists</code>. All I want to do is to <strong>get rid of <code>static</code></strong>.</p>
  
  <p>Here is a little example.</p>
  
  <ol>
  <li>I create a simple Person object (main thread)</li>
  <li>main thread calls a method to create 3 threads</li>
  <li>each of those threads calls another method to fill its own <code>listOfLists</code> with data</li>
  <li>at the end of that method I add the threads own <code>listOfLists</code> to the <code>static listOfLists</code></li>
  <li>and that is what I want to avoid. There must not be any static.</li>
  <li>I just want to have those thread data available in my mainthread and to proceed with the application on the main thread.</li>
  </ol>
  
  <p>Edit: it has to be .NET 3.5</p>
  
  <p>Edit2: One solution seems to be to provide the reference of listOfLists (of main thread) as parameter when starting the threads. Are there any other ways to solve such a threadingproblem?</p>
  
  <hr>
  
  <pre><code>namespace Threading
  {
      class Program
      {
          static void Main(string[] args)
          {
              Person p = new Person("mainThread");
              p.startMultiThreading();
          }                   
   }
  </code></pre>
  
  <hr>
  
  <pre><code>class Person
  {
      public String name;
      public List&lt;String&gt; privatePet;
      public List&lt;List&lt;String&gt;&gt; listOfLists = new List&lt;List&lt;string&gt;&gt;();
      public static List&lt;List&lt;String&gt;&gt; familyPets = new List&lt;List&lt;string&gt;&gt;();
  
      public Person(String n) { name = n; } //constructor
      public Person() { }
  
      public void startMultiThreading()
      {
          List&lt;Thread&gt; list_threads = new List&lt;Thread&gt;();
          for (int i = 0; i &lt; 3; i++)
          {
              Person multiThreadPerson = new Person("multi: " + i); //create new Person
              Thread t = new Thread(multiThreadPerson.fill_List); //create new Thread
              t.Name = multiThreadPerson.name; //name the Thread with PersonName
  
              list_threads.Add(t);
              list_threads[i].Start(); //new Person is calling fill_list()
          }
  
          for (int i = 0; i &lt; list_threads.Count; i++)
          {
              list_threads[i].Join();
          }
  
          familyPets.Add(new List&lt;string&gt;{"this is mainthread again"});
          foreach (List&lt;String&gt; list in familyPets)
          {
              list.ForEach(e =&gt; Debug.WriteLine(e));
          }
      }
  
      public void fill_List()
      {
          privatePet = new List&lt;string&gt;();
          lock (familyPets)
          {
              for (int i = 0; i &lt; 20; i++)
              {
                  privatePet.Add("dog :" + Thread.CurrentThread.Name);
                  privatePet.Add("cat :" + this.name);
                  listOfLists.Add(privatePet);
              }
              familyPets.AddRange(listOfLists); //adding up all listOfLists to the mainthread listOfLists
          }//lock 
      }
  }
  </code></pre>