Markus Knappen Johansson
Markus Knappen Johansson

Reputation: 1630

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?

Upvotes: 9

Views: 2395

Answers (1)

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

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

Upvotes: 9

Related Questions