I have the web api app with this method
public class TestController
{
public UserView Get()
{
var testThread = new Thread(() => { throw new Exception(); });
testThread.Start();
Thread.Join();
Thread.Sleep(1000);
return null;
}
}
When i request this method, my application pools breaks. I receive this window 
And broken an application pools.
When i have this method
var task = new Task(() => { throw new Exception(); });
task.Start();
everything is good. Why?
I guess because Task does not throw exceptions it return it as a property of Task object. If you does not check Task result it even won't be thrown until application start shutdown sequence and then it will be thrown as UnhandledException. I guess in Thread is different
That's just how ASP.NET works. If a thread has an unhandled exception that isn't tied to a request, then it will bring the application down. This is described on Phil Haack's blog. He mentioned a detail followup post explaining specifically why comes down, but I was unable to find the followup post.
Depending on what you need to do in another thread, you can use an alternative such as async code, QueueBackgroundWorkItem, or a scheduler that has been specifically designed to work around these limitations such as Hangfire, WebBackgrounder or Quartz.NET.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With