Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will ASP.NET kill a new thread?

I've tried some googling on this subject but i would like to have som more info.

I'm trying to start a new thread inside an ASP.NET app that will take care of some work that takes long time. If I put this in my web.config:

<httpRuntime executionTimeout="5" />

A regular request will timeout after 5 secounds. Remember this is for testing. When I start a new thread from the code:

    var testThread = new Thread(new ThreadStart(CustomClass.DoStuffThatTakesLongTime));
    testThread.Start();

This thread will run for longer than 5 secounds, that's what I want. BUT. For how long will it run? Let's say this thread takes 5h (just as an example). When will the thread be killed? Will it run until the app pool is recycled? Or is there anything else that kills this thread?

like image 360
Markus Knappen Johansson Avatar asked Jan 10 '12 14:01

Markus Knappen Johansson


People also ask

Do threads end automatically?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.

Does ASP Net use threads?

ASP.NET ThreadingThe worker thread pool handles all incoming requests and the I/O Thread pool handles the I/O (accessing the file system, web services and databases, etc.).

How do you stop a thread in net core?

In C#, a thread can be terminated using the Abort() method. The Abort() throws ThreadAbortException to the thread in which it is called. Due to this exception, the thread is terminated.


1 Answers

ASP.NET has no knowledge of the thread that you have created - it will run until the AppPool is recycled or it completes.

Since ASP.Net has no knowledge of this thread however, it could be aborted quite abruptly at any point if the server thinks that it should recycle the AppPool, this would not be a good thing! Phil Haack wrote a blog post on how to subscribe to the 'AppDomainIsGoingDown' event.

In regards to what can cause this, I'd recommend reading this blog post by Tess Ferrandez, but in a nutshell they are:

  • It has been scheduled to do so
  • Machine.Config, Web.Config or Global.asax are modified
  • The bin directory or its contents is modified
  • The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the setting in machine.config or web.config (by default this is set to 15)
  • The physical path of the virtual directory is modified
  • The CAS policy is modified
  • The web service is restarted
  • (2.0 only) Application Sub-Directories are deleted
like image 151
Rich O'Kelly Avatar answered Sep 21 '22 05:09

Rich O'Kelly