取消执行方法的正确方法 [英] Proper way of cancel execution of a method
问题描述
可能重复:
如何中止/取消TPL任务?
我有一个方法,它需要一些时间来执行,因此我将结果作为回调返回.我的方法如下:
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?
感谢您的回答.我现在记得取消令牌的事.令牌的事情很难记住.我想我会使用这种方法:
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.我的Visual Studio放置了很多空间.随意设置代码格式:)
PS. My Visual Studio places a lot of spaces. Feel free to format the code :)
推荐答案
通过在任务中指定您的功能来尝试使用取消令牌 http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx
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();
}
这篇关于取消执行方法的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!