title: Create Multiple Threads in a for Loop in C#
body: <p>I have a test client where I can specify the number of threads to create. I run a for loop based on the number selected by the user and start my threads. I store the threads in a List so that I can monitor/kill the threads at a later stage. When I run the loop, I can see that all the threads have ThreadState.Running. However, I always see fewer threads being created. Kindly Help</p>
  
  <pre><code>    private void btnReadMTNPServer_Click(object sender, EventArgs e)
      {
          DisableTestButtons();
          SetValuesFromControls();
          ResetThreads();
          _metrics.Clear();
          _runTest = true;
          _keys = GetTestKeysList();
  
          for (var i = 0; i &lt; _numClients; i++)
          {
              var index = i;
              TestThreads.Add(RunNamedPipeThread(index));
              AppendConsoleText(string.Format("ThreadState: {0}", TestThreads[index].ThreadState));
              Thread.Sleep(50);
          }
  
          AppendConsoleText(string.Format("Starting {0} Clients. Each Reading {1} Keys", TestThreads.Count, nudTestReadKeyCount.Value));
      }
  
  
      private Thread RunNamedPipeThread(int i)
      {
          var thread = new Thread(RunNamedPipeServerTest)
          {
              Name = i.ToString("D3")
              //IsBackground = true
          };
          while (thread.ThreadState != ThreadState.Running)
              thread.Start(i);
          return thread;
      }
  
  private void RunNamedPipeServerTest(object i)
      {
          var id = (int)i;
          var iteration = 0;
          var list = new List&lt;short&gt;();
          var client = new PipeClient("DHM");
          {
              client.Start();
              while (_runTest)
              {
                  var startTimeStamp = DateTime.Now;
                  foreach (var key in _keys.TakeWhile(key =&gt; _runTest))
                  {
                      list.Add(client.GetValue(key));
                      if (_delayPerKey &gt; 0)
                          Thread.Sleep(_delayPerKey);
                  }
                  var finishTimeStamp = DateTime.Now;
                  //Keep the client connected using KeepAlive.
                  client.KeepAlive();
                  _metrics.Add(new PerformanceMetric(id.ToString("D3"), startTimeStamp, finishTimeStamp));
                  AppendConsoleText(string.Format("{3}:{0}:Start:{1}, Took {2} ms", id.ToString("D3"), startTimeStamp.ToString("mm:ss.ffff"), finishTimeStamp.Subtract(startTimeStamp).TotalMilliseconds, iteration++));
                  //Wait before sending request for data.
                  Thread.Sleep(_testFrequency);
              }
              client.Stop();
          }
          if (cbxDisplayValues.Checked)
              AppendConsoleText(string.Format("{0}", string.Join(",", list)));
      }
  
  
      private void ResetThreads()
      {
          if (TestThreads == null)
              TestThreads = new List&lt;Thread&gt;();
          _runTest = false;
          TestThreads.ForEach(t =&gt; t.Abort());
          TestThreads.Clear();
      }
  
  
      private void AppendConsoleText(string text)
      {
          if (txtConsole.InvokeRequired)
          {
              Invoke(new MethodInvoker(() =&gt; txtConsole.AppendText(string.Format("{0}{1}", text, Environment.NewLine))));
          }
          else
          {
              txtConsole.AppendText(string.Format("{0}{1}", text, Environment.NewLine));
          }
      }
  </code></pre>
  
  <p>This is the result that I always see in the text box. There are 5 threads running but only 3 are writing out. This happens randomly though. Sometimes 5 write out as expected and sometimes they don't.</p>
  
  <pre><code>ThreadState: Running
  ThreadState: Running
  ThreadState: Running
  ThreadState: Running
  ThreadState: Running
  Starting 5 Clients. Each Reading 1000 Keys
  0:002:Start:53:55.5393, Took 187.4653 ms
  0:003:Start:53:55.6331, Took 125.0191 ms
  0:004:Start:53:55.7424, Took 109.4014 ms
  1:002:Start:53:58.7539, Took 62.4642 ms
  1:003:Start:53:58.7851, Took 62.5031 ms
  1:004:Start:53:58.8789, Took 46.8898 ms
  2:002:Start:54:01.8316, Took 62.4693 ms
  2:003:Start:54:01.8629, Took 125.0298 ms
  2:004:Start:54:01.9410, Took 93.7239 ms
  3:002:Start:54:04.9149, Took 52.3515 ms
  3:003:Start:54:04.9985, Took 62.5211 ms
  3:004:Start:54:05.0454, Took 46.8765 ms
  4:002:Start:54:07.9954, Took 46.77 ms
  4:003:Start:54:08.0736, Took 46.7811 ms
  4:004:Start:54:08.1203, Took 46.8748 ms
  5:002:Start:54:11.0636, Took 46.7713 ms
  5:003:Start:54:11.1417, Took 62.4283 ms
  5:004:Start:54:11.1885, Took 46.8748 ms
  6:002:Start:54:14.1308, Took 39.5712 ms
  6:003:Start:54:14.2173, Took 54.1113 ms
  6:004:Start:54:14.2486, Took 47.9687 ms
  7:002:Start:54:17.1964, Took 51.7147 ms
  7:003:Start:54:17.2795, Took 62.3954 ms
  7:004:Start:54:17.3107, Took 46.8722 ms
  8:002:Start:54:20.2644, Took 46.77 ms
  8:003:Start:54:20.3581, Took 46.77 ms
  8:004:Start:54:20.3738, Took 46.7721 ms
  9:002:Start:54:23.3386, Took 46.77 ms
  9:003:Start:54:23.4324, Took 46.7704 ms
  9:004:Start:54:23.4480, Took 46.7969 ms
  </code></pre>
  
  <p><strong>Edit #1:</strong>
  What is happening is that 5 threads say they are running. however, if you see closely below the line that says 'Starting 5 Clients. Each Reading 1000 Keys' there are 3 entries for each iteration 0, 1, 2... every three seconds. I am expecting 5 as 5 threads are running.</p>
  
  <p>As per @jackncoke, I did try using this instead of the for loop: </p>
  
  <pre><code>Parallel.For(0, _numClients, i =&gt; RunNamedPipeServerTest(i)); 
  </code></pre>
  
  <p>For some reason, it is creating only one thread.</p>
  
  <p>Then I also tried:</p>
  
  <pre><code>Parallel.For(0, _numClients, i =&gt; RunNamedPipeThread(i));
  </code></pre>
  
  <p>this works, however it still does not create the right number of threads. Also, if you look at method <em>RunNamedPipeThread</em>, I am still explicitly creating the thread for <em>RunNamedPipeServerTest</em>.</p>