Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
insert duplicate link
Source Link

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

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

Post Closed as "exact duplicate" by Sergey Berezovskiy, pstrjds, Tono Nam, svick, Graviton
added 1888 characters in body
Source Link
Tono Nam
  • 36.5k
  • 86
  • 330
  • 496

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 :)

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 :)

normalized code
Source Link
Victor Zakharov
  • 26.6k
  • 18
  • 95
  • 158
Loading
Source Link
Tono Nam
  • 36.5k
  • 86
  • 330
  • 496
Loading