title: Updating UI from the async method
body: <p>I have a MainWindow with a <em>TextBlock</em> and a <em>Button</em> controls on it, by clicking on which the <em>RunAsync(int)</em> method is called. It was doing some calculations, so the process took quite a long time and blocked the UI. I was able to move it to an asynchronous thread, but I am completely stuck on how best to implement updating the interface from this method every iteration of the <em>For</em> loop. Using the progress bar, for example.</p>
  <p>At the moment i have the following code:</p>
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow() =&gt; InitializeComponent();
  
      private async Task&lt;List&lt;int&gt;&gt; RunAsync(int par)
      {
          return await Task.Run(() =&gt;
          {
              List&lt;int&gt; list = new List&lt;int&gt;();
              for (int i = 0; i &lt; par;  i++)
              {
                  // some code, that takes quite a long time
                  Thread.Sleep(500); 
                  list.Add(i);
              }
              return list;
          });
      }
      
      private async void StartBtn_Click(object sender, RoutedEventArgs e)
      {
          int count = (await RunAsync(5)).Count;
          label.Text = count.ToString();
      }
  }
  </code></pre>