title: Threadpool issue Using C#
body: <p>I'm working on my university project. One of main requirement is to use <code>multithreading</code> (user can choose threads numbers). </p>
  
  <p>I'm new in C# and based on internet research. I choose <code>ThreadPool</code>. </p>
  
  <p>I spent a lot of time observing how the threads act using parallel watch in VS and i have no idea how this thing works. For example <code>threadNumber = 10</code> but parallel watch shows only 4 activated threads.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public void calculateBeta()
      {
          var finished = new CountdownEvent(1);
          for (int i = 0; i &lt; threadNumber; i++)
          {
              finished.AddCount();
              ThreadPool.QueueUserWorkItem(
              (state) =&gt;
              {
                  try
                  {
                     doSth();
                  }
                  finally
                  {
                      finished.Signal();
                  }
              });
          }
          finished.Signal();
          finished.Wait();
      }
  </code></pre>
  
  <p>What am I doing wrong? I tried to test this code with many different values of threads number and it didn't work as i looked for.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code> private void myTask(object index)
      {
  
              int z = (int)index;
              double[] result = countBeta(createTableB(z), createTableDiagonalA(z));
              int counter = 0;
              if ((rest != 0) &amp;&amp; (z == threadNumber - 1))
              {
                  for (int j = z * numbersInRow; j &lt; (z + 1) * numbersInRow + rest; j++)
                  {
                      N[j] = result[counter];
                      counter++;
                  }
              }
              else
              {
                  for (int j = z * numbersInRow; j &lt; (z + 1) * numbersInRow; j++)
                  {
                      N[j] = result[counter];
                      counter++;
                  }
              }
              threads[z] = true;
      }
  
  
  public void calculateBeta()
      {
          N = new double[num];
          setThreadNumber(2);
          checkThreadNumber();
          setNumberInRow();
          setRest();
          threads = new bool[threadNumber];
          for (int i = 0; i &lt; threadNumber; i++)
          {
              Thread thread = new Thread(this.myTask);
              thread.IsBackground = true;
              thread.Start(i);
          }
          while (!checkThreads())
          {
          }
      }
  
  private bool checkThread()
      {
          bool result = true;
          for (int i = 0; i &lt; threads.Length; i++)
          {
              if (!threads[i])
                  result = false;
          }
  
          return result;
      }
  
  static void Main(string[] args)
      {
  
  
          Jacobi jacobi = new Jacobi();
          Console.WriteLine("Metoda Jacobiego");
          Console.WriteLine("Rozwiazywanie ukladu n-rownan z n-niewiadomymi Ax=b");
          jacobi.getNum();
          jacobi.getA();
          jacobi.getB();
          jacobi.calculateBeta();
          jacobi.calculateM();
          jacobi.calculateX();
          jacobi.countNorms();
          Console.ReadLine();
      }
  </code></pre>
  
  <p>I need results from calculateBeta to further calculations. Sometimes threads are not finished yet but the program moves forward without data that need to be provided by threads. I'm using bool variable now but this solution is not an elegant way to deal with it(Creating bool table, checking if all thread are fnished) How can i manage with that in a different way?</p>