user204588
user204588

Reputation: 1633

Progress Updates on Long Process with Javascript

I have an asp.net page that calls a dll that will start a long process that updates product information. As the process is running, I want to provide the user with constant updates on which product that process is on and the status of the products. I've been having trouble getting this to work. I add information to a log text file and I was thinking that I'd redirect to a new page and have that page use Javascript to read from the text file every few seconds. My question is has anyone else tried this and does it work fairly well?

Upvotes: 1

Views: 718

Answers (3)

Mike Trpcic
Mike Trpcic

Reputation: 25649

Rather than outputting the status to a text based log file (or, in addition to using the log file; you can use both), you could output the status to a Database table. For example, structure the table as so:

Queue:
  id (Primary Key)
  user_id (Foreign Key)
  product_id (Foreign Key) //if available
  batch_size //total set of products in the batch this message was generated from
  batch_remaining //number remaining in this batch
  message

So now you have a queue. Now when a user goes on a particular page, you can do one of two things:

  1. Grab some relevant data from the Queue and display it in the header. This will be updated every time the page is refreshed.
  2. Create an AJAX handler that will poll a web service to grab the data at intervals. This way you can have the status update every 15 or 20 seconds, even if the user is on the same page without refreshing.

You can delete the rows from the Queue after they've been sent, or if you want to retain that data, add another column called sent and set it to true once the user has seen that data.

Upvotes: 0

Andras Vass
Andras Vass

Reputation: 11638

  • Make the long process so that it updates some state about the progress - status, last product processed, etc.
  • Once started, you might want to update the progress on only say every 50th item processed, to spare resources (you might not, it depends on what you want) on the ASP.NET side.
  • If the processing is associated with the current session, you might want to put the state in the session. If it is not - e.g. global - put it in some global state.
  • Then poll the state once in a while through Ajax from javascript, using e.g. JSON and update the UI accordingly.
  • Make sure you use proper locking when accessing the shared state.
  • Also, try to keep the state small (store only what is absolutely required to get the data for the js gui).

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

I would use Ajax and poll that text file for updates.

Upvotes: 2

Related Questions