title: C# Threading - Reading and hashing multiple files concurrently, easiest method?
body: <p>I've been trying to get what I believe to be the simplest possible form of threading to work in my application but I just can't do it.</p>
  
  <p>What I want to do: I have a main form with a status strip and a progress bar on it. I have to read something between 3 and 99 files and add their hashes to a string[] which I want to add to a list of all files with their respective hashes. Afterwards I have to compare the items on that list to a database (which comes in text files).
  Once all that is done, I have to update a textbox in the main form and the progressbar to 33%; mostly I just don't want the main form to freeze during processing.</p>
  
  <p>The files I'm working with always sum up to 1.2GB (+/- a few MB), meaning I should be able to read them into byte[]s and process them from there (I have to calculate CRC32, MD5 and SHA1 of each of those files so that should be faster than reading all of them from a HDD 3 times).</p>
  
  <p>Also I should note that some files may be 1MB while another one may be 1GB. I initially wanted to create 99 threads for 99 files but that seems not wise, I suppose it would be best to reuse threads of small files while bigger file threads are still running. But that sounds pretty complicated to me so I'm not sure if that's wise either.</p>
  
  <p>So far I've tried workerThreads and backgroundWorkers but neither seem to work too well for me; at least the backgroundWorkers worked SOME of the time, but I can't even figure out why they won't the other times... either way the main form still froze.
  Now I've read about the Task Parallel Library in .NET 4.0 but I thought I should better ask someone who knows what he's doing before wasting more time on this.</p>
  
  <p>What I want to do looks something like this (without threading):</p>
  
  <pre><code>List&lt;string[]&gt; fileSpecifics = new List&lt;string[]&gt;();
  
  int fileMaxNumber = 42; // something between 3 and 99, depending on file set
  
  for (int i = 1; i &lt;= fileMaxNumber; i++)
  {
      string fileName = "C:\\path\\to\\file" + i.ToString("D2") + ".ext"; // file01.ext - file99.ext
      string fileSize = new FileInfo(fileName).Length.ToString();
      byte[] file = File.ReadAllBytes(fileName);
      // hash calculations (using SHA1CryptoServiceProvider() etc., no problems with that so I'll spare you that, return strings)
      file = null; // I didn't yet check if this made any actual difference but I figured it couldn't hurt
      fileSpecifics.Add(new string[] { fileName, fileSize, fileCRC, fileMD5, fileSHA1 });
  }
  
  // look for files in text database mentioned above, i.e. first check for "file bundles" with the same amount of files I have here; then compare file sizes, then hashes
  // again, no problems with that so I'll spare you that; the database text files are pretty small so parsing them doesn't need to be done in an extra thread.
  </code></pre>
  
  <p>Would anybody be kind enough to point me in the right direction? I'm looking for the easiest way to read and hash those files quickly (I believe the hashing takes some time in which other files could already be read) and save the output to a string[], without the main form freezing, nothing more, nothing less.</p>
  
  <p>I'm thankful for any input.</p>
  
  <p>EDIT to clarify: by "backgroundWorkers working some of the time" I meant that (for the very same set of files), maybe the first and fourth execution of my code produces the correct output and the UI unfreezes within 5 seconds, for the second, third and fifth execution it freezes the form (and after 60 seconds I get an error message saying some thread didn't respond within that time frame) and I have to stop execution via VS.</p>
  
  <p>Thanks for all your suggestions and pointers, as you all have correctly guessed I'm completely new to threading and will have to read up on the great links you guys posted.
  Then I'll give those methods a try and flag the answer that helped me the most. Thanks again!</p>