John S
John S

Reputation: 8331

Long running server process updating a label on the client side. AJAX ASP.Net

I have a long running server side process (gets data via a web service in batches as part of a do/while loop). On each pass through the loop I want to push a status message back to a label control in an update panel on the client.

I've tried assigning the value to the label in code behind and then using a timer control to refresh the update panel every minute. But that doesn't seem to work.

Any ideas or suggestions?

TIA

Upvotes: 3

Views: 3748

Answers (4)

Jake
Jake

Reputation: 3075

Have your server process Write and Flush a

<script>
  /* jquery to update an element on the client page */
</script>

to update the element on the page with the current status. You may have to pad this with spaces so it adds up to enough bytes to get flushed correctly (like, say 1000bytes) . This can come out even after the . Your client may have a bit of an issue with this, if the stuff inside the script tags is very verbose, and you have a LOT of updates, but if you keep it just to a handful of updates it should be fine (like.... every 10th of a percent through), otherwise the filesize of the final output may be in the high megabytes range.

Upvotes: 0

zincorp
zincorp

Reputation: 3282

You're on the right track in your comment. Consider spawning the work in a separate thread and tracking its progress through the Session/Cache. You could even go a step further and encapsulate the task and all of its threading logic into a LongRunningTask class, and in your polling code just check ((LongRunningTask)Cache["MyLongRunningTask"]).TaskStatus or something along those lines (of course - don't use that exact code to retrieve the status =))

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88054

You might consider a slightly different approach.

First, get rid of the timer. Then use Response.Write to output where you are in the process. I.E.: Processing Item Number 234

After the response.write, do a Response.Flush. This forces the server to write the currently cached response object to the browser.

Upvotes: 0

Andrew Siemer
Andrew Siemer

Reputation: 10278

I suggest using a simple jQuery.get call to a page that reads the status from your server side process probably by way of a session variable. Then the client would make the request. The failure is that you are having your server side try to push content to the client (forcing the update) from the server side...as the long running process continues to run. To my knowledge you can't push via http at your whim...the request must be made by the browser!

Upvotes: 1

Related Questions