简体   繁体   English

在C#中中止文件导入操作

[英]Abort file import operation in c#

I want to implement abort/cancel file import operation in c#. 我想在C#中实现中止/取消文件导入操作。 I am using below code to achieve this - 我正在使用下面的代码来实现这一目标-

CancellationTokenSource ctokensource;
            if (_importtasks.TryGetValue(Convert.ToInt32(jobHistoryRecno), out ctokensource))
                ctokensource.Cancel();

Even though, it execute above function successfully, It fails to cancel the task. 即使成功执行以上功能,也无法取消任务。 still import is keep going. 进口仍在继续。

Could you please help me how to achieve abort/cancel functionality on file import. 您能否帮助我如何在文件导入时实现中止/取消功能。

Regards Rajiv Kumar 问候拉吉夫·库马尔

Cancel just informs attached token (which you can get with CancellationTokenSource.Token property) about cancellation. Cancel只是通知附加的令牌(您可以通过CancellationTokenSource.Token属性获得)有关取消的信息。 You should cancel your operation manually by either checking CancellationToken.IsCancellationRequested or calling CancellationToken.ThrowIfCancellationRequested() , the second method should be used if operation is wrapped with Task . 您应该通过检查CancellationToken.IsCancellationRequested或调用CancellationToken.ThrowIfCancellationRequested()来手动取消操作,如果操作与Task包装,则应使用第二种方法。 Also you can register the necessary callback with CancellationToken.Register method, which will be executed when cancellation requested. 您还可以使用CancellationToken.Register方法注册必要的回调,该方法将在请求取消时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM