Improve your ASP.NET Website User Experience: Flush Down your partial Response
There are times when your website page needs to do some heavy work on the server side and you don’t want your user to wait till the work is complete to get a response.
In these cases you can use the Response.Write & Response.Flush methods, and here is a short example (on the HTML source side):
<% Response.Write("1st response..."); %>
<% Response.Flush(); %>
<% System.Threading.Thread.Sleep(2000); %>
<% Response.Write("2nd response..."); %>
<% Response.Flush(); %>
<% System.Threading.Thread.Sleep(2000); %>
<% Response.Write("3rd response..."); %>
<% Response.Flush(); %>
<% Response.Write("4th response..."); %>
<% Response.Flush(); %>
When you’ll run this code, you’ll find that the page is loaded with “1st response…” message, after 2 more seconds, the “2nd response…” would popup, and after 2 more seconds, the last two messages would appear.
This means that we can show our website users some response while our server doing some work.
This will work also on the code-behind, but needs a little tweak..
Response.Write("1st response...");
Response.Flush();
Response.Flush();
System.Threading.Thread.Sleep(2000);
Response.Write("2nd response...");
Response.Flush();
Response.Flush();
System.Threading.Thread.Sleep(2000);
Response.Write("3rd response...");
Response.Flush();
Response.Write("4th response...");
Response.Flush();
You probably notice that each Response.Flush appear twice, this is not a mistake - The reason is that sometimes when “you have to use more than a handful of toilet paper, it’s best to flush twice”..
Hope you’ll find it useful.