title: How to use multi threading in a For loop
body: <p>I want to achieve the below requirement; please suggest some solution.</p>
  
  <pre><code>string[] filenames = Directory.GetFiles("C:\Temp"); //10 files
  
  for (int i = 0; i &lt; filenames.count; i++)    
  {
      ProcessFile(filenames[i]); //it takes time to execute    
  }
  </code></pre>
  
  <p>I wanted to implement multi-threading. e.g There are 10 files. I wanted to process 3 files  at a time (configurable, say <code>maxthreadcount</code>). So 3 files will be processed in 3 threads from the for loop and if any thread completes the execution, it should pick the next item from the for loop. Also wanted to ensure all the files are processed before it exits the for loop.</p>
  
  <p>Please suggest best approach.</p>