取消任务

时间:2015-08-25 13:53:52

标签: c# task

我需要取消使用dll中长时间运行的方法的任务(本例中为MethodFromDll()) 我可以在哪里调用cancellationToken.ThrowIfCancellationRequested()方法来取消这个任务?

tokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = tokenSource.Token;        

t = Task.Factory.StartNew(() => {
try {
    ...some code
    // I need to cancel this task manually if method not answer
    // or there is no wish to wait
    MethodFromDll(); 
   ...some code
    } catch {
         ...some code
    }
}, cancellationToken);

0 个答案:

没有答案