title: Adding cancel ability and exception handling to async code
body: <p>I have this sample code for async operations (copied from the interwebs)</p>
  
  <pre><code>public class LongRunningTask
  {
      public LongRunningTask()
      { 
      //do nowt
      }
      public int FetchInt()
      {
          Thread.Sleep(2000);        
          return 5;
      }
  }
  
  public delegate TOutput SomeMethod&lt;TOutput&gt;();
  public class GoodPerformance
  {
      public void BeginFetchInt()
      {
          LongRunningTask lr = new LongRunningTask();
          SomeMethod&lt;int&gt; method = new SomeMethod&lt;int&gt;(lr.FetchInt);
  
          // method is state object used to transfer result
          //of long running operation
          method.BeginInvoke(EndFetchInt, method);
  
      }
  
      public void EndFetchInt(IAsyncResult result)
      {        
          SomeMethod&lt;int&gt; method = result.AsyncState as SomeMethod&lt;int&gt;;        
          Value = method.EndInvoke(result);
  
      }
      public int Value { get; set; }
  }
  </code></pre>
  
  <p>Other async approaches I tried required the aysnc page attribute, they also seemed to cancel if other page elements where actioned on (a button clicked), this approach just seemed to work.</p>
  
  <p>I’d like to add a cancel ability and exception handling for the longRunningTask class, but don’t erm, really know how.</p>