简体   繁体   English

有什么优雅的方法可以取消/停止长时间运行的线程?

[英]Is there any elegant way to cancel/stop a long running thread?

I know about CancellationToken support in TPL, but (IMO) that could work only if I have appropriate token.ThrowifCancellationRequested(), which is not possible in my scenario. 我知道TPL中对CancellationToken的支持,但是(IMO)仅在我具有适当的token.ThrowifCancellationRequested()时才有效,在我的方案中这是不可能的。

Is there any other elegant way (other than aborting the thread altogether) to achieve this? 是否有其他优雅的方法(除了完全中止线程外)来实现这一目标?

I do not care about the transactional state of the thread. 我不在乎线程的事务状态。 I do not care at what state the thread is stopped/cancelled. 我不在乎线程停止/取消的状态。

Thanks for your interest! 谢谢你的关注!

More Details: I need to spawn a new thread (Task/Threadpool/Thread) to call an API which calls multiple stored procedures to fetch near 1million records, and then it does some decoration (business calculation) on each of the records. 更多详细信息:我需要产生一个新线程(任务/线程池/线程)来调用API,该API调用多个存储过程以获取近100万条记录,然后对每条记录进行一些修饰(业务计算)。 In the end, this API returns a data table which is a business significant report. 最后,此API返回一个数据表,该数据表是具有业务意义的报告。 I want to give the users the option to cancel this processing mid-way, if needed. 如果需要,我想让用户选择中途取消此处理。 For instance, if wrong of set of filters are set. 例如,如果设置了错误的过滤器。 The API only does the read-only operations, so no transaction management is required. 该API 执行只读操作,因此不需要事务管理。

To do this properly, you should use built-up methods in a Task class via a cancellation token, or use cancel a background worker. 若要正确执行此操作,应通过取消标记在Task类中使用内置方法,或使用取消后台工作程序。 Or, you may try to use a volatile bool to check if a user request has been cancelled. 或者,您可以尝试使用volatile布尔检查用户请求是否已取消。 However, volatile bools aren't behaving consistently across different architectures (single/multi processor). 但是, volatile布尔在不同的体系结构(单/多处理器)中表现不一致。 Alternatively you may use *ResetEvent s to indication that cancel was called. 或者,您可以使用*ResetEvent指示取消被调用。 This all needs to be done in the API itself, otherwise how would it know when to check for a cancellation condition? 所有这些都需要在API本身中完成,否则它将如何知道何时检查取消条件? You won't be able to cancel it gracefully. 您将无法正常取消它。

Of course, do not ever call Abort on a thread - that's evil and may leave the whole application in an undefined state, unless you plan to restart whole application, don't call it. 当然,永远不要在线程上调用Abort这很邪恶,并且可能会使整个应用程序处于未定义状态,除非您打算重新启动整个应用程序,否则不要调用它。

Bonus suggestion 奖金建议

Also consider doing all the processing work on the database side. 还可以考虑在数据库方面进行所有处理工作。 Do not bring more than a very small amount of records to the client side, say 1k is a good practical threshold. 不要将太多的记录带到客户端,例如1k是一个很好的实用阈值。 Consider a situation where many users request different reports that all load a server that much - this doesn't scale well. 考虑一下这样一种情况,其中许多用户请求不同的报告,这些报告都给服务器带来了很大的负担-这不能很好地扩展。 Moreover servers are very efficient with data processing and they have data at theirs side - lots of optimisation is done (CPU line caching, fast memory access etc.). 此外,服务器在数据处理方面非常高效,并且它们拥有自己的数据-完成了许多优化(CPU行缓存,快速内存访问等)。

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

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