title: Proper way of cancel execution of a method
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4783865/how-do-i-abort-cancel-tpl-tasks">How do I abort/cancel TPL Tasks?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a method that takes some time to execute therefore I return the result as a callback. My method looks like:</p>
  
  <pre><code>public static void DoWork( Action&lt;object&gt; onCompleteCallBack)
  {
    Task.Factory.StartNew( () =&gt; {
      // Do work
      onCompleteCallBack(someResult);
    });
  }
  </code></pre>
  
  <p>Now I will like to be able to stop executing that method in case the user does not want to wait. As a result this is what I have worked out:</p>
  
  <pre><code>static void Main ( string[] args )
  {            
    var cancelMethod = DoWork( x =&gt;
    {                
      // method completed
      Console.Write( x.ToString() );
    });
    Thread.Sleep( 5000 ); // some time passes 
  
    // then user decides to abort method
    cancelMethod();
    Console.Read();
  }
  
  static Action DoWork ( Action&lt;object&gt; onCompleteCallBack )
  {
    bool stopExecuting = false;
    Task.Factory.StartNew( () =&gt;
    {
      for ( var i = 0 ; i &lt; 100000 ; i++ ) 
      {
        Thread.Sleep( 1 );
        if ( stopExecuting )
        {
          onCompleteCallBack( "Method aborted!" );
          return;
        }
      }         
      onCompleteCallBack( "Method completed successfully" );
    } );
    return () =&gt; { stopExecuting = true; };
  }
  </code></pre>
  
  <p>What will be a more appropriate way of aborting the execution of a method? </p>
  
  <h2>Edit</h2>
  
  <p>Thanks for your answers. I remember now about the cancellation token thing. The token thing is hard to remember. I think I will use this approach:</p>
  
  <pre><code>    static void Main ( string[ ] args )
      {
          Action abortTask;
  
          DoWork( methodCompleted, out abortTask );
  
          Thread.Sleep( 5000 ); // some time passes then user decides to abort method
  
          // cancel execution of method
          abortTask( );
  
          Console.Read( );
      }
  
      static void methodCompleted ( object msg )
      {            
          Console.Write( msg.ToString( ) );  
      }
  
      static void DoWork ( Action&lt;object&gt; onCompleteCallBack, out Action abortThisTask )
      {
          bool stopExecuting = false;
  
          abortThisTask = ( ) =&gt; { stopExecuting = true; };
  
          Task.Factory.StartNew( ( ) =&gt;
          {
              for ( var i = 0 ; i &lt; 100000 ; i++ ) 
              {
                  Thread.Sleep( 1 );
  
                  if ( stopExecuting )
                  {
                      onCompleteCallBack( "Method aborted!" );
                      return;
                  }
              }
  
              onCompleteCallBack( "Method completed successfully" );
          } );            
      }
  
      // Overloaded method
      static void DoWork ( Action&lt;object&gt; onCompleteCallBack )
      {
          Action abortTask;
          DoWork( onCompleteCallBack ,out abortTask );
      }
  </code></pre>
  
  <p>Will it be better to use the approaches you guys suggested on the answers to this question vs. This approach. I like this approach better. I think it is easier to read than the cancellation token one.</p>
  
  <p>PS.
  My Visual Studio places a lot of spaces. Feel free to format the code :)</p>