acidzombie24 acidzombie24 - 19 days ago 7
ASP.NET (C#) Question

Why am i getting "Thread was being aborted" in asp.net?

I am not sure why this happens and I never explicitly abort threads, so it's a bit of a surprise. But I log Exceptions and I am seeing:


System.Threading.ThreadAbortException - Thread was being aborted.


It appears to happen in a call to
System.Threading.WaitHandle.WaitOne
. I am not sure how far this Exception goes. I don't think my threads ever terminate because I catch log and swallow the error.

Does anyone know why I'm getting these errors? Perhaps it's when I am forcefully terminating my server or asking it to reboot? If it isn't then what might be causing them?

Answer

If you spawn threads in Application_Start, they will still be executing in the application pool's AppDomain. If an application is idle for some time (meaning that no requests are coming in), or certain other conditions are met, ASP.NET will recycle the entire appdomain. When that happens, any threads that you started from that AppDomain, including those from Application_Start, will be aborted.

Lots more on application pools and recycling in this question: What exactly is Appdomain recycling

If you are trying to run a long-running process within IIS/ASP.NET, the short answer is usually "Don't". That's what Windows Services are for.

Comments