views:

51

answers:

3

Hello,

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?

+2  A: 

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

Dustin Laine
I wouldn't, the logfile gets too big, you can't afford to continuously poll it again every few seconds.
Quandary
a very straightforward approach is to have the page periodically send AJAX queries to the ASPNET server, which keeps track of the status in some way, maybe by re-reading the log file, or maybe in some other way. It's up to the ASPNET server to do that efficiently.
Cheeso
How big are we talking? The server side method behind the AJAX call should be able to handle it. Without knowing more about the log file it is hard to say, but it there is a datetime stamp on each log entry you could only return the results that changed.
Dustin Laine
@durilai: That would require writing the logfile in an SQL database.
Quandary
A: 
  • 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).
andras
A: 

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.

Mike Trpcic
Yes, AJAX, a web-service/method and continous polling is the only way [for IE]. You have to use a database and write the logfile into the database (with timestamp/datetime) and delta-poll continuously. It's much faster with reverse-ajax [and reverse-ajax doesn't require a database, in fact, you can just do as on a console] in an iframe, but reverse-ajax doesn't work in IE.
Quandary