JG in SD JG in SD - 9 months ago 50
C# Question

What happens to running tasks when IIS recycles

To help with performance of the clients, I offload the processing of a request onto a task. This is done because the processing usually takes a bit of time, and I don't want the clients to be waiting for a while just to get the 200 response. The web service that offloads the work onto a task is always processing posts.

public void ProcessRequest(HttpContext context)
// check for bad requests -- return 400

// get copy of the context input stream

Task.Factory.StartNew(() =>

private void ProcessRequest(Stream inputStream)
// process input stream
catch(Exception ex)
// any server error that would normally result in 500 response are not
// exposed to the clients, the clients are to see 200 when the server
// encounters an error

So my question is what happens to these tasks when IIS recycles, or when the web site is being stopped.

Answer Source

When IIS recycles, it waits for all that threads to finish and exits - up to the timeout value that have on pool. After that time out they kills all running threads and start again.

So you may set a signaling to your threads to stop when the application request to shut down at globa.asax using the Application_End functions.