title: Update progress bar from async method
body: <p>I am trying to understand better how can I update a windows forms progress bar from an async operation but I am getting some unexpected behavior from that.</p>
  
  <p>Basically I am having a button which should after is being clicked to update a progress bar and then set it back to 0 once the progress bar gets 100% updated.</p>
  
  <p>This is my code:</p>
  
  <pre><code>    private async void button1_Click(object sender, EventArgs e)
      {
          await CallMethodAsync().ContinueWith((prevTask) =&gt; 
          {
              prevTask.Wait();
              progressBar1.Invoke(new Action(() =&gt; { progressBar1.Value = 0; }));
          });
      }
  
      private static async Task ExecuteMethodAsync(IProgress&lt;double&gt; progress = null)
      {
          double percentComplete = 0;
          bool done = false;
  
          while (!done)
          {
              if (progress != null)
              {
                  progress.Report(percentComplete);
              }
  
              percentComplete += 10;
  
              if(percentComplete == 100)
              {
                  done = true;
              }
          }
      }
  
      private async Task CallMethodAsync()
      {
          var progress = new Progress&lt;double&gt;();
          progress.ProgressChanged += (sender, args) =&gt; { progressBar1.Increment(10); };
          await ExecuteMethodAsync(progress);
      }
  </code></pre>
  
  <p>Having this implementation the progress bar is not being updated at all even if I call "Wait()" on the operation that should update the value of the progress bar.</p>
  
  <p>If i remove this part of code:</p>
  
  <pre><code>progressBar1.Invoke(new Action(() =&gt; { progressBar1.Value = 0; }));
  </code></pre>
  
  <p>the progress bar gets updated but it remains all the time like that, and I want to set it back to 0 once it was entirely filled so that I can update it again when I click again the button.</p>
  
  <p>Could someone please explain me what am I doing wrong ? </p>