简体   繁体   English

如何在ASP.NET Core中杀死线程

[英]How to kill a thread in asp.net core

I am writing an application using asp.net core in which I need to create a new thread as I have an always running loop. 我正在使用asp.net内核编写应用程序,由于我一直在运行循环,因此需要在其中创建一个新线程。 At some point, I need to mill this thread. 在某个时候,我需要研磨该线程。 In asp.net, Thread.abort was the solution, but it is removed in asp.net core. 在asp.net中,使用Thread.abort是解决方案,但在asp.net核心中将其删除。 What is the alternative solution for this? 有什么替代解决方案?

Do not create your own thread for something like this! 不要为此类创建自己的线程!

There is a built-in method for using long running tasks in asp.core. 在asp.core中有一个内置方法可以使用长时间运行的任务。 You should read about this here . 您应该在这里阅读有关内容。

You should create a class which derives from BackgroundService . 您应该创建一个从BackgroundService派生的类。 Using this class is the easiest way to create a background-service that implements IHostedService . 使用此类是创建实现IHostedService的后台服务的最简单方法。 You can then add this to your program by calling services.AddHostedService<YourBackgroundService>() in the ConfigureServices method. 然后,可以通过在ConfigureServices方法中调用services.AddHostedService<YourBackgroundService>()将其添加到程序中。

Note: In the page I linked, they use AddSingleton instead of AddHostedService . 注意:在我链接的页面中,他们使用AddSingleton而不是AddHostedService In .net core 2.1 and above you should use AddHostedService , not AddSingleton (there are some exceptions but we're talking in general here). 在.NET核心2.1及以上,你应该使用AddHostedService ,不AddSingleton (有一些例外,但我们一般这里所说的)。 See this answer for why that is. 请参阅此答案以了解原因。

If you implement your background-service like this, the shutdown of the additional thread will be handled for you. 如果您这样实现后台服务,则将为您处理其他线程的关闭。 In your implementation of ExecuteAsync you need to just check if you should stop executing with the provided CancellationToken . ExecuteAsync的实现中,您只需检查是否应该停止使用提供的CancellationToken执行即可。 You should also use asnyc implementations where possible and provide the CancellationToken there as well so the thread can end gracefully. 您还应该尽可能使用asnyc实现,并在那里提供CancellationToken ,以便线程可以正常结束。 You will never need to call Thread.Abort or even have access to the Thread itself; 您将不需要调用Thread.Abort甚至不需要访问Thread本身。 it's all done in the background for you. 这一切都在后台为您完成。

Since this is not a direct answer to the question you asked but more of a correction of what you're probably doing wrong to get into this situation in the first place, I first wanted to make this a comment. 由于这并不是您所提问题的直接答案,而是更多地纠正了您可能首先出于这种情况而做错的事情,所以我首先想对此发表评论。 However it's just too long and there are too many things to mention that's why I made this into an answer. 但是,这太长了,有太多事情要提到,这就是为什么我将其作为答案。

Hope this helps. 希望这可以帮助。

The cleanest way to do this via a flag that is set by the "killing" thread and checked periodically by the thread that needs to be killed. 通过由“ killing”线程设置并由需要被杀死的线程定期检查的标志进行此操作的最干净的方法。 Thread.Abort() is not a reliable way to do it; Thread.Abort()不是一种可靠的方法。 even the MSDN says Calling this method usually terminates the thread. 甚至MSDN都说调用此方法通常会终止线程。

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

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