title: multithreading vs async for directory traverse
body: <p>I am writing a code to calculate the size of the folder, for now I have this code</p>
  <pre><code>private long CalculateSize(string root)
      {
          long size = 0;
          Stack&lt;string&gt; dirs = new Stack&lt;string&gt;(20);
  
          if (!System.IO.Directory.Exists(root))
          {
              throw new ArgumentException();
          }
          dirs.Push(root);
  
          while (dirs.Count &gt; 0)
          {
              string currentDir = dirs.Pop();
              string[] subDirs;
              try
              {
                  subDirs = Directory.GetDirectories(currentDir);
              }
              catch (UnauthorizedAccessException)
              {
                  continue;
              }
              catch (DirectoryNotFoundException e)
              {
                  continue;
              }
  
              string[] files = null;
              try
              {
                  files = Directory.GetFiles(currentDir);
              }
  
              catch (UnauthorizedAccessException)
              {
                  continue;
              }
  
              catch (DirectoryNotFoundException e)
              {
                  continue;
              }
              foreach (string file in files)
              {
                  try
                  {
                      FileInfo fi = new System.IO.FileInfo(file);
                      size += fi.Length;
                  }
                  catch (FileNotFoundException e)
                  {
                      Console.WriteLine(e.Message);
                      continue;
                  }
              }
              foreach (string str in subDirs)
              {
                  dirs.Push(str);
              }
          }
          return size;
      }
  </code></pre>
  <p>It works fine, but it is so slow if I provide some big starting directory like &quot;C:&quot; or &quot;C:\Windows&quot;.
  I had an idea to do something like:</p>
  <pre><code>string[] subDirs;
  subDirs = Directory.GetDirectories(currentDir);
  </code></pre>
  <p>And then start a new thread or parallel process or smth like that for each directory in subDirs, but I have no idea what should I use, and how to implement it. Also, do I have to do it for each level of subdirectories? I mean for example I have &quot;C:&quot; it has 3 subdirectories like &quot;Windows&quot;, &quot;ProgramFile&quot;, and &quot;ProgramFIlex86&quot;, so I am starting a multithreading processes for each, and each of these directories have 50 more directories, do I have to start a new process for each?</p>