DCSIMG
Improve your ASP.NET Website User Experience: Flush Down your partial Response - Itai Goldstein

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.

Published Sunday, February 08, 2009 8:46 AM by itai

Comments

# Improve your ASP.NET Website User Experience: Flush Down your partial

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Sunday, February 08, 2009 9:02 AM by DotNetKicks.com

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

What about using Response.Buffer??

Sunday, February 08, 2009 10:31 AM by Maor David-Pur

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

Response.Buffer value should be left as it is - true (the default).

Nice catch Maor.. :)

Sunday, February 08, 2009 10:57 AM by itai

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

look nice

those it mean you open new threads just to show him the application is working?

Sunday, February 08, 2009 4:52 PM by former cubical partner

# Improve your ASP.NET Website User Experience: Flush Down your &#8230;

Pingback from  Improve your ASP.NET Website User Experience: Flush Down your &#8230;

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

Well Ido (aka former cubical partner..),

You don't need to open new threads.

You just flush the currently response (what you've added to the response till the flushing point), using the buffering feature of the HttpResponse.

Monday, February 09, 2009 10:02 AM by itai

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

Example from code-behind does not work. All content from Response.Write appears after about 4 sec.. First code works fine.

I read somewhere that Page buffer is empty before event Page.Render() ?

Any ideas?

Monday, February 09, 2009 3:16 PM by tomek

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

Interesting.

Can you send me your implementation page? (+the code behind file of course..)

Wednesday, February 11, 2009 9:41 AM by itai

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

Thanks for response...

Project is in ASP.NET 3.5 SP 1

As I wrote string "1st response...2nd response...3rd response...4th response... " (and string test and button) appears all after about 4 sec (after button click and page postback). I tried move Response.Write's to Page_Load event - the same...

regards from Poland ;)

***aspx***

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">

<html xmlns="www.w3.org/.../xhtml">

<head runat="server">

   <title></title>

</head>

<body>

   <form id="form1" runat="server">

   <div>

       test

       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

   </div>

   </form>

</body>

</html>

//***aspx***

***cs***

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{

   protected void Page_Load(object sender, EventArgs e)

   {

   }

   protected void Button1_Click(object sender, EventArgs e)

   {

       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();

   }

}

//***cs***

Wednesday, February 11, 2009 6:49 PM by tomek

# re: Improve your ASP.NET Website User Experience: Flush Down your partial Response

I would call the use of this technique quite dated and would provide a poor user experience. This is what I would call an old school website experience... with the use of AJAX and webservice calls you can do heavy lifting with your code while the site remaining responsive.

This might be fine for when your writing single lines of text but a web page is far more complicated then a single line of text and wrapping code is very important...if your div's don't close for example... your site will look broken and you might as well have let the user wait for all the code to complete executing then show a broken page.

Just my thoughts.

Thursday, February 12, 2009 10:32 PM by mi

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: