title: Winforms recursive file scan blocks UI
body: <p>I have a program that searches the given directory and adds all the files to a list view. My problem is that the ui thread gets stuck while the search is busy. I have tried using tasks but can’t get it to work in async. The list view must be updated after each file has been found.</p>
  
  <p>I have done a lot of reading about the TPL and how to use it but can’t get it to work in this case. I got it to work where the processing of data is in one method that create a task to process it. Can any one tel me what is wrong in the code below and how to fix it? </p>
  
  <p>Here is my code:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      Task.Run(() =&gt;
      {
          WalkDirectory(new DirectoryInfo(drive));
      });
  }
  
  public void testTaskUpdateLabel(string labelTeks)
  {
      Task taskUpdateLabel = new Task(() =&gt;
      {
          label4.Text = labelTeks;
      });
      taskUpdateLabel.Start(uiScheduler);
  }
  
  public void testTaskUpdateLabel(string labelTeks)
  {
      Task taskUpdateLabel = new Task(() =&gt;
      {
          label4.Text = labelTeks;
      });
      taskUpdateLabel.Start(uiScheduler);
  }
  
  public bool WalkDirectory(DirectoryInfo directory)
  {
      if (directory == null)
      {
          throw new ArgumentNullException("directory");
      }
      return this.WalkDirectories(directory);
  }
  
  private bool WalkDirectories(DirectoryInfo directory)
  {
      bool continueScan = true;
      continueScan = WalkFilesInDirectory(directory);
  
      if (continueScan)
      {
              DirectoryInfo[] subDirectories = directory.GetDirectories();
  
              foreach (DirectoryInfo subDirectory in subDirectories)
              {
                  try
                  {
                      if ((subDirectory.Attributes &amp; FileAttributes.ReparsePoint) != 0)
                      {
                          continue;
                      }
                      if (!(continueScan = WalkDirectory(subDirectory)))
                      {
                          break;
                      }
                  }
                  catch (UnauthorizedAccessException)
                  {
                      continue;
                  }
              }
      }
      if (continueScan)
      {
          testTaskUpdateLabel(directory.FullName);
      }
      return continueScan;
  }
  
  private bool WalkFilesInDirectory(DirectoryInfo directory)
  {
      bool continueScan = true;
      // Break up the search pattern in separate patterns
      string[] searchPatterns = _searchPattern.Split(';');
  
      // Try to find files for each search pattern
      foreach (string searchPattern in searchPatterns)
      {
          if (!continueScan)
          {
              break;
          }
          // Scan all files in the current path
          foreach (FileInfo file in directory.GetFiles(searchPattern))
          {
              try
              {
                  testTaskUpdate(file.FullName);
              }
              catch (UnauthorizedAccessException)
              {
                  continue;
              }
          }
      }
      return continueScan;           
  </code></pre>
  
  <p>}</p>