Reputation: 3601
I maintain a ASP.NET web application that causes a user's network connection to reset for several seconds when it executes a procedure. Therefore, the page request times out on the user's end as they never receive the web application's response (the connection dies before it gets the response packet).
To resolve this situation, I was contemplating having the ASP.NET page execute an asynchronous function that incorporates a Thread.Sleep(5000); // sleep for 5 seconds before executing the connection reset
This way, the browser has 5 seconds to receive the page's response before their connection resets.
I have concerns with using Thread.Sleep and asynchronous functions in ASP.NET though. I've never attempted to do it before, so I'm unsure of the potential problems it may cause. Does anyone see potential problems with starting an asynchronous thread that contains a Thread.Sleep
in an ASP.NET application? If so, can you think of a better solution?
Upvotes: 5
Views: 5595
Reputation: 3046
It is generally not a good idea to put the thread to sleep on a web server, because the threads are limited. In your specific case you need one client, starting ~1-2 requests per second, to block all your threads...
You should use a timer, as others proposed, and ofcource a AsyncPage
/ AsyncHttpHandler
.
Upvotes: 1
Reputation: 2077
Placing Thread.Sleep in an asynchronous method can contribute to ThreadPool Starvation, as it blocks one of a limited number of threads for several seconds - that thread could be off servicing client requests.
Instead, why not create a timer that fires after five seconds? Same effect, just register your delayed work in the timer's event.
Upvotes: 3
Reputation: 30185
Though I find your problem statement too vague to address properly, I can say that you should use System.Threading.Timer instead of a Sleep statement. The former is performant, the latter will keep one of your CPU core's in a busy state (very bad!).
Upvotes: 1