Royi Namir
Royi Namir

Reputation: 148514

Apppool recycle and Asp.net with threads?

I have made an asp.net page which executes a long sp. lets say the the function that executes the sp is called Func1.

Ive met with that problem :

If i ran Func1 in the same thread ( normal execution), the apppool won't recycle itself since he's seeing it as a busy/working.

But if I execute Func1 in another thread - so the apppool recycle's itself after the time that is set here :

enter image description here

My question is : why is that ?

is it true that if i run a command synchronously , so app is active and not eligible for apppool recycle ? And if i create it in a new thread so it does eligible for apppool recycle ?

why is that ? Does the thread is less important then the main thread ?

Upvotes: 4

Views: 2585

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

ASP.NET maintains a list of thread pool threads that it is using to service requests. It knows it can recycle the app domain when none of its threads are active.

If you create a thread or use a thread pool thread without the knowledge of ASP.NET, it will not detect that your thread is active and may recycle.

When it recycles, it unloads the AppDomain which causes a ThreadAbortException to be thrown on your thread.


The normal solution to your requirements is to have a windows service that is controlled by the web app. This is obviously in a separate process and so is not affected by the web app recycling. However, this is a non-trivial exercise.

The quick-and-dirty solution is to asynchronously start a web request from within your web app. The page that starts the operation can then return. The "hidden" page that was called can block until the SP has completed. As I said, this is a nasty-but-easy solution.

Upvotes: 6

Related Questions