views:

82

answers:

1

I am executing time consuming task in a new thread.

 ParameterizedThreadStart pts = new ParameterizedThreadStart(WorkingFoo);
 Thread thread = new Thread(pts);

The WorkingFoo executing the task and keeps track of the progress steps (it can return the total number of "steps" and the current step).

I want to display this information in a progress bar (simple div or ajax control, I don't mind).

I don't want, ofcourse, visually refresh the page that will display the progress.
Also I want to reduce as much as possible the number of postbacks.

So how do I do that?
How to show the current progress from new thread on the web page?

Also the page that shows the progress can be closed and re-opned and still get the current status.

No jQuery please.

+2  A: 

First, Unless you are certain you need to, Don't start a new thread like that from an ASPNET page or application. Use the thread pool.

See Advantage of using Thread.Start vs QueueUserWorkItem


Regarding your question - how to get progress updates in the browser, without refreshing the page... I assume you know how to use the progress controls in jQuery. Your question centers around how to connect such a progress control to the server-side logic. The only way to do it is via requests, I suggest ajax, from the browser to the server.

If I were you, I would provide a way for the worker thread to put its status into a resource on the server - either the user session, or a database, or something like that. Then, have the ajax request from the page query that status.

In a REST scenario, the status might be available at

http://myserver/Application/WorkItem12345/status

...where WorkItem12345 is an id that points to the worker or task that is happening asynchronously. Doing an HTTP GET on that URL from the page to the server, you'd get a JSON result indicating the status, maybe it looks like this:

{"message": "working", "progress":56}

or

{"message": "completed", "progress":100}

or

{"message": "fault", "progress":14}

etc.

There must be an application on the server that responds to http://myserver/Application/WorkItem12345/status and replies with the appropriate message. This status logic should look in the slot or resource where the Worker thread posts its status - whatever you chose to use - and then construct ad return the JSON.


Addendum
If you don't want to use jQuery, the mechanics are the same, but you need to write your own browser-side UI widget that can display a status update. In the simple case it is a simple div with html content that shows the JSON response. if you want to get fancier you could make it an actual rectangular bar that grows as the work proceeds.

But how to build that widget, is the topic for another question.


See also: Dino Esposito's MSDN Magazine article on the topic

Cheeso
well, I think I can use thread pool cause the number threads that could be started is limited(only users in a limited role can access this page) .
kenny
Sorry, forgot to note: no jQuery please.
kenny
I will use session to store the the progress status.regarding the widget it's not a problem I'll handle it.I need more explanation regarding the AJAX's requests.
kenny
Why do I need to use HTTP GET to get the status parameters? Why can't I just store in callback parameter, and then call callback function on client?
kenny
Because the client is a client. It sends requests, it does not receive requests. You can work around this with various approaches, but in general, the client gets to "drive". The client asks, and the server responds.
Cheeso
I am talking about using PageRequestManager from the client. Is it bad practice?
kenny