views:

462

answers:

1

Hope I can explain this correctly.

I have a process, that outputs step by step messages (i.e., Processing item 1... Error in item 2 etc etc).

I want this to be outputted to the user during the process, and not at the end.

I pretty sure i need to do this with threading, but can't find a decent example.

Thanks in advanced.

+5  A: 

It's not a threading issue, but a web browser UI issue. You want the browser to render the status as you are doing work on the server. In theory you could do something like:

Response.Write("something");
Response.Flush();

but the Flush() won't ensure the browser actually renders your code at that moment. In reality you cannot control how data is cached/chunked/buffered underway from the server to the browser. So each update should be a 'full' http transaction.

One way, and common one, is to use AJAX to achieve this. The user clicks a button which starts some background work, and you have a javascript timer which polls (makes requests) to check the status of the work, and updates the client browser.

Check out Real-Time Progress Bar With ASP.NET AJAX for doing an ajax progress indicator with ajax and .net.

There's an excellent example of creating a progress bar with a http handler in this article: http://www.asp101.com/articles/matt/progressbar/default.asp

To prove my point, the following code works in Firefox, but not in IE or Chrome:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = false;
    Response.Clear();
    Response.Write("<html><body>");
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Response.Write("</body></html>");
    Response.End();
}
Mikael Svenson
I have seen a really neat way that used threads before... Just can't remember where, and how to do it. It's basically started a method running, and every so often, the next progress message is displayed
Danny
Your work will execute on a thread which updates it's progress state. Then your client will poll for the state. But as I mentioned, you could in theory kick the work off in a thread and write progress with Response.Write and Response.Flush, but you are not guaranteed that it's actually flushed to the browser and rendered before the entire request finishes.
Mikael Svenson
I don't think that Response.Write works in a thread.However, something is coming back at the item I saw before, and I think it may have used AJAX to pool the status client side.
Danny
Added a code sample to show the use of Flush, and that it won't work for all browsers. So kick of your work on a background thread, and have the client poll the status. Of course, a thread might die if IIS recycles the app pool, so for a 100% solution long running work should be run outside IIS in a service of some kind.
Mikael Svenson