title: WPF async await
body: <p>I'm trying to use async click event:</p>
  
  <pre><code>private async void Button_Click(object sender, RoutedEventArgs e)
  {
      MessageBox.Show(Thread.CurrentThread.ManagedThreadId + "   click_begin");
      await changeProgressBarAsync(ProgresBar_mainLoad);
      MessageBox.Show(Thread.CurrentThread.ManagedThreadId + "    click_end");
  }
  
  public Task changeProgressBarAsync(ProgressBar pb)
  {
      return Task.Factory.StartNew(() =&gt;
      {
          MessageBox.Show(Thread.CurrentThread.ManagedThreadId + "    task");
          int count = 100;
          while (count-- &gt; 0)
          {
              pb.Dispatcher.Invoke(() =&gt;
              {
                  pb.Value += 1;
              });
  
              Thread.Sleep(10);
          }
      });
  }
  </code></pre>
  
  <p>Following the async-await C# logic, lines</p>
  
  <pre><code>MessageBox.Show(Thread.CurrentThread.ManagedThreadId + "    task");
  </code></pre>
  
  <p>and</p>
  
  <pre><code>MessageBox.Show(Thread.CurrentThread.ManagedThreadId + "    click_end");
  </code></pre>
  
  <p>should show equal thread Id, 
  but this program show me</p>
  
  <ul>
  <li>1 click_begin</li>
  <li>5 task</li>
  <li>1 click_end</li>
  </ul>
  
  <p>So why it happens?</p>
  
  <p>I did same test in console app</p>
  
  <pre><code>static void Main(string[] args)
  {
      TestClick();
      Console.ReadKey();
  }
  
  public static async void TestClick()
  {
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " it is TestClick1");
      await Worker();
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " it is TestClick2");
  }
  
  public static Task Worker()
  {
      return Task.Factory.StartNew(() =&gt;
      {
          Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " it is Task");
      });
  }
  </code></pre>
  
  <p>And it show me</p>
  
  <ul>
  <li>1 it is TestClick1</li>
  <li>3 it is Task</li>
  <li>3 it is TestClick2</li>
  </ul>