I am trying to send some content to the client before doing some lengthy work:
Response.Write("Processing...");
Response.Flush();
System.Threading.Thread.Sleep(5000);
Response.Write("Finish");
Response.End();
In Firefox it works as expected but in IE8, Safari and Chrome it waits until all the code is processed and then shows the whole text.
I have tried sending a better formed HTML like the below sample but I get the same results:
Response.Write("<html><head><title>test</title></head><body>Processing...</body></html>");
Response.Flush();
System.Threading.Thread.Sleep(5000);
Response.Write("Finish");
Response.End();
Thank you!
Also, be aware that if your IIS server is compressing the output with GZIP, then it will seem to ignore all Response.Flush
calls.
This is turned on by default in IIS7 and on Windows 7.
And, if you are testing with Fiddler, be sure to turn on "Streaming" mode, or Fiddler will collect the flushed HTML and hold it until the connection is completed.
The problem you are facing is that the response you are sending is still incomplete. Even though you flush whatever is in the buffers to the browser, it is still up to the browser to wait for the response end or process what it's got so far - hence the difference between browsers.
What's even worse is that you can expect the same behavior from some intermediate nodes concentrators, firewalls, etc. located on the internet between your server and the browser.
The bottom line is that if you want to ensure that browser does something with your data stream you have to complete it with Response.End.
In other words if you want to send some of your response data first and delay sending the rest your better option is to break the response in two, complete the first one and download the second part separately
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