title: Using threads to speed up a process
body: <p>I inherited an application that had a long list of items. </p>
  
  <p>This took a few hours to loop through so I thought I could optimize it by running threads. </p>
  
  <p>example:</p>
  
  <pre><code>        foreach (var d in data)
          {
              try
              {
                  // do alot of logic, takes time
              }
              catch (Exception e)
              {
                // catch error
  
              }
  
          }
  </code></pre>
  
  <p>I thought i could optimize it by: </p>
  
  <pre><code>        foreach (var d in data)
          {
              try
              {
                  ExecutionMethodThread(d);
              }
              catch (Exception e)
              {
  
              }
  
          }
  
      private  void ExecutionMethodThread(data d)
      {
          Thread thread = new Thread(() =&gt; MethodThread(d));
          thread.Start();
          thread.Join();
  
      }
  </code></pre>
  
  <p>Although when I set a breakpoint in the foreach-loop I still notice that it waits for each item to finish before moving on to the next. 
  This is obviosly not what I want since I was hoping for this list to just run 
  through the loop and I would have multiple items having their own threads simultaniosly. </p>
  
  <p>How do I achieve this?</p>
  
  <p>Clarify:</p>
  
  <blockquote>
    <p>Loop begins</p>
    
    <p>Item one starts a thread and executs logic</p>
    
    <p>item two DOES NOT WAIT for item one, gets its own thread and executs
    logic etc</p>
  </blockquote>