3

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. 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. What is the alternative solution for this?

5
  • an always running loop in a web context seems like a really bad idea. I'd imagine you are are spawning memory leaks all over your web server. What is the alternative solution for this don't do this at all...ever?! Either way it'd be nice to see some code. You should also use Task not Thread classes Commented Aug 2, 2019 at 11:42
  • 1
    Calling Thread.Abort() is almost never a solution. It can corrupt the run-time for the remaining threads. You should only ever call it when you're trying to crash out of an app completely. Commented Aug 2, 2019 at 11:44
  • 1
    The loop is to monitor a sensor, thats why I cannot run away from it Commented Aug 2, 2019 at 11:47
  • The idea of "an always running thread" is not consistent with "at some point, I need to kill this thread." Commented Aug 2, 2019 at 16:35
  • 2
    Threads, by definition, in any language, communicate by acting on shared variables. If you forcibly kill a thread, you risk leaving the shared variables in an inconsistent state. Forcibly killing one thread while allowing other threads to continue running is always bad practice. Threads should cooperate. If thread A needs thread B to die, then thread A should send a message asking thread B to die, and thread B should be designed to handle the request by cleanly shutting itself down. Commented Aug 2, 2019 at 16:40

2 Answers 2

7

Do not create your own thread for something like this!

There is a built-in method for using long running tasks in asp.core. You should read about this here.

You should create a class which derives from BackgroundService. Using this class is the easiest way to create a background-service that implements IHostedService. You can then add this to your program by calling services.AddHostedService<YourBackgroundService>() in the ConfigureServices method.

Note: In the page I linked, they use AddSingleton instead of 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). 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. You should also use asnyc implementations where possible and provide the CancellationToken there as well so the thread can end gracefully. You will never need to call Thread.Abort or even have access to the Thread itself; 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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. Thread.Abort() is not a reliable way to do it; even the MSDN says Calling this method usually terminates the thread.

2 Comments

In general, this is entirely correct. However OP is talking about asp.net core and there you really shouldn't create your own threads. See my answer for what to do instead.
In .NET Core and .NET 5 and onwards Thread.Abort is not supported. They marked it as obsolete in .NET 5, because it doesn't abort the thread and should not be used: learn.microsoft.com/en-us/dotnet/core/compatibility/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.