title: winforms async await task UI lockup
body: <p>I am trying to read the contents of a directory that has over 100k+ files. Each file will be parsed eventually. Since this is a long time consuming task I want to provide feedback to the end user that the program has not "frozen", This is an MVP pattern project setup. </p>
  
  <p>I am using async Task and IProgress to provide feedback to the UI, but either methods below lock up the UI and I can't figure out why..</p>
  
  <pre><code>    private async void MainFormViewOnStartProcessingFiles(object sender, EventArgs eventArgs)
      {
          // The Progress&lt;T&gt; constructor captures our UI context,
          //  so the lambda will be run on the UI thread.
          var asyncProgress = new Progress&lt;int&gt;(percent =&gt;
          {
              EventAggregator.Instance.Publish(new DisplayMainFormWaitBarMessage($"Async found {percent} files..."));
          });
  
          // GetDirectoryFiles is run in the thread pool
          await Task.Run(() =&gt; GetDirectoryFilesAsync(asyncProgress));
  
  
          var syncProgress = new Progress&lt;int&gt;(percent =&gt;
          {
              EventAggregator.Instance.Publish(new DisplayMainFormWaitBarMessage($"Sync found {percent} files..."));
          });
  
          GetDirectoryFiles(syncProgress);
  
      }
  </code></pre>
  
  <p>This updates the UI </p>
  
  <pre><code>        private void DisplayWaitingBarMessage(DisplayMainFormWaitBarMessage obj)
      {
          _mainFormView.DisplayWaitBarMessage(obj.DisplayText);
      }
  </code></pre>
  
  <p>This is my Async and Non Async code to read the files into a queue</p>
  
  <pre><code>        private async Task GetDirectoryFilesAsync(IProgress&lt;int&gt; taskProgress)
      {
          await Task.Run(() =&gt;
          {
              try
              {
                  var directoryPath = Directory.GetCurrentDirectory() + @"\..\..\..\FUG_2017\";
                  var _listOfDirectoryFiles = Directory.GetFiles(directoryPath).Take(100);
  
                  var fileCount = 0;
                  foreach (var filePath in _listOfDirectoryFiles)
                  {
                      _filesToProcessQueue.Enqueue(filePath);
                      fileCount++;
  
                      taskProgress?.Report(fileCount);
                  }
              }
              catch (Exception exc)
              {
                  FlushAndExit(exc);
              }
          });
      }
  
      private void GetDirectoryFiles(IProgress&lt;int&gt; taskProgress)
      {
          try
          {
              var directoryPath = Directory.GetCurrentDirectory() + @"\..\..\..\FUG_2017\";
              EventAggregator.Instance.Publish(new DisplayMainFormWaitBarWithMessage(ElementVisibility.Visible, $"Inspecting path {directoryPath}"));
  
              var _listOfDirectoryFiles = Directory.GetFiles(directoryPath).Take(1000);
  
              var fileCount = 0;
              foreach (var filePath in _listOfDirectoryFiles)
              {
                  _filesToProcessQueue.Enqueue(filePath);
                  fileCount++;
  
                  EventAggregator.Instance.Publish(new DisplayMainFormWaitBarWithMessage(ElementVisibility.Visible, $"Loaded file {fileCount} of {_listOfDirectoryFiles.Count()} to Queue..."));
                  taskProgress?.Report(fileCount);
              }
  
              EventAggregator.Instance.Publish(new DisplayMainFormWaitBarWithMessage(ElementVisibility.Visible, $"Loaded {_listOfDirectoryFiles.Count()} files to Queue..."));
          }
          catch (Exception exc)
          {
              FlushAndExit(exc);
          }
      }
  </code></pre>