7

Possible Duplicate:
How do I abort/cancel TPL Tasks?

I have a method that takes some time to execute therefore I return the result as a callback. My method looks like:

public static void DoWork( Action<object> onCompleteCallBack)
{
  Task.Factory.StartNew( () => {
    // Do work
    onCompleteCallBack(someResult);
  });
}

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:

static void Main ( string[] args )
{            
  var cancelMethod = DoWork( x =>
  {                
    // 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<object> onCompleteCallBack )
{
  bool stopExecuting = false;
  Task.Factory.StartNew( () =>
  {
    for ( var i = 0 ; i < 100000 ; i++ ) 
    {
      Thread.Sleep( 1 );
      if ( stopExecuting )
      {
        onCompleteCallBack( "Method aborted!" );
        return;
      }
    }         
    onCompleteCallBack( "Method completed successfully" );
  } );
  return () => { stopExecuting = true; };
}

What will be a more appropriate way of aborting the execution of a method?

Edit

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:

    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<object> onCompleteCallBack, out Action abortThisTask )
    {
        bool stopExecuting = false;

        abortThisTask = ( ) => { stopExecuting = true; };

        Task.Factory.StartNew( ( ) =>
        {
            for ( var i = 0 ; i < 100000 ; i++ ) 
            {
                Thread.Sleep( 1 );

                if ( stopExecuting )
                {
                    onCompleteCallBack( "Method aborted!" );
                    return;
                }
            }

            onCompleteCallBack( "Method completed successfully" );
        } );            
    }

    // Overloaded method
    static void DoWork ( Action<object> onCompleteCallBack )
    {
        Action abortTask;
        DoWork( onCompleteCallBack ,out abortTask );
    }

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.

PS. My Visual Studio places a lot of spaces. Feel free to format the code :)

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470

5 Answers5

5

try using cancellation token by specifying it your function in task http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx

CancellationTokenSource tokenSource = new CancellationTokenSource();
 Task.Factory.StartNew( () => {
    // Do work
    onCompleteCallBack(someResult);
  }, tokenSource.Token);

private void cancel_Click(object sender, RoutedEventArgs e)
{
    tokenSource.Cancel();

}
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
1

Look at the CancellationToken and Albahari's guide to cancelling thread safely

pstrjds
  • 16,840
  • 6
  • 52
  • 61
1

There is an overload of the StartNew() method that accepts a CancellationToken. Here's an example: http://msdn.microsoft.com/en-us/library/dd997396.aspx. The basics are that you create and then pass in this token to your method (or refer to it from the parent thread; it's thread-safe). Your parallel method then must, at convenient times (like within a loop), check to see if the token indicates the task has been cancelled. If so, it gracefully exits.

KeithS
  • 70,210
  • 21
  • 112
  • 164
1

Tasks allow you to pass in a CancellationToken. You can call ct.Cancel() in order to set the flag for cancellation. Within your actual execution logic, you can add in checks at certain "checkpoints" which test for if (ct.IsCancellationRequested) and return the method from there or alternatively call ct.ThrowIfCancellationRequested().

Lunyx
  • 3,164
  • 6
  • 30
  • 46
  • What's the point of `ThrowIfCancellationRequested`? I know this approach comes from MSDN, but why not just Throw New `UserCancellationException` or whichever applies? – Victor Zakharov Nov 20 '12 at 22:06
  • I'm not exactly sure as I'm quite new to this myself; I'm just sharing what I learned. Personally, I am just returning something different instead of throwing an exception. The benefit to throwing an exception in general would be to have cleanup logic in the exception handler. – Lunyx Nov 20 '12 at 22:08
  • I am also learning, but never heard of `ThrowIfCancellationRequested`. If you explain benefits it can provide, especially in regard to context described on MSDN, I'd personally give you a +1 on this answer. :) – Victor Zakharov Nov 20 '12 at 22:09
  • [This answer](http://stackoverflow.com/a/7343338/897326) is interesting. Looks like `ThrowIfCancellationRequested` = `if (token.IsCancellationRequested) throw new OperationCanceledException(token);` even though the latter is much more readable, i.e. you clearly see type of exception being thrown. – Victor Zakharov Nov 20 '12 at 22:12
  • It seems to me that the difference between an `OperationCanceledException` and a `UserCancellationException` is what classes they are derived from as well as their use. According to MSDN documentation, a `UserCancellationException` is thrown "when the user cancels an operation during the GetToken(CardSpacePolicyElement[], SecurityTokenSerializer) call." That method resides in the `System.IdentityModel.Selectors` namespace while `OperationCanceledException` derives from `System.Exception` and also contains the original `CancellationToken` that was canceled. – Lunyx Nov 20 '12 at 22:20
  • What you just said sounds like some voodoo magic, but thanks for looking into this for me. +1. – Victor Zakharov Nov 20 '12 at 22:22
0

http://msdn.microsoft.com/en-us/library/dd537607(v=vs.100).aspx would probably be a good place to start. Depending on what you're going for.

Michael Dunlap
  • 4,300
  • 25
  • 36