title: Cancel an async task
body: <p>For example I have something like this. When I am clicking on first button it start's async process and then I am clicking second button it start's second process. But I need only one process to work after clicking on each button. How can I cancel other process?</p>
  
  <pre><code>namespace WpfApplication55
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          TestCombo TC = new TestCombo();
          public MainWindow()
          {
              DataContext = TC;
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              TC.Begin(60);
          }
  
          private void Button_Click1(object sender, RoutedEventArgs e)
          {
              TC.Begin(120);
          }
      }
  
      public class TestCombo:INotifyPropertyChanged
      {
          private int someData;
          public int SomeData 
          {
              get { return someData; }
              set { someData = value; RaisePropertyChanged("SomeData"); }
          }
  
          public void StartCount(int input)
          {
              SomeData = input;
              while (input&gt;0)
              {
                  System.Threading.Thread.Sleep(1000);
                  input -= 1;
                  SomeData = input;
              }
          }
  
          public void Begin(int input)
          {   
              Action&lt;int&gt; Start = new Action&lt;int&gt;(StartCount);
              IAsyncResult result = Start.BeginInvoke(input, null, null);
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          private void RaisePropertyChanged (string info)
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  }
  </code></pre>