views:

50

answers:

1

When I hit the run button (in my Default.aspx), a process starts (this process contacts a webservice to get some files, etc). How do I:

  1. Ensure that only a single process is running at a time (i.e. if I refresh the browser, I don't want to start the process a second time)

  2. Track progress - there are 4 points of the process (at 25%, 50%, 75%, 100%) that I want to track, and when each part completes, I want to update the progress bar. I have a status object for the running process, but the question is how to update the progress bar automatically?

  3. Do I need to use threads to achieve the above two?

A: 

Answering them in reverse order:

  1. Yes. And you should have an object to encapsulate your thread.

  2. You could have a public member of your object which contains the % done of your process. You could poll this periodically. The way that you would do this from your HTML page is probably an AJAX request to make your progress bar the smoothest.

  3. You need to do some thread synchonization. Maybe through a Mutex. Here's a good example of how to do this (scroll down to Mutex).

Keltex
Thanks. Could you please explain why I would need threads in this scenario? I have only a single process and no parallel activity. If I store the Process object (or just the Status object, which has a IsRunning property) in the HttpApplicationState, I can access the running process each time the browser is refreshed right?
Prabhu
@Swami. You actually do have more than one thread. If you run your request on the same thread that the page is processed on, then the page will never get returned to the user. So that's why you want to fire off a new thread to handle your task. Also you don't want to tie up IIS threads with a background activity like your processing activity.
Keltex
@Keltex. Thanks...one more question...I want to make sure that if the page is opened from a browser on a different computer, and if the process is running, the page should indicate the status, instead of present the Run button. Would this be possible?
Prabhu
@Swami Yes. Because when the page is loaded, you check the mutex to see if the process is running. If so, don't display the button and instead display the status. The singleton object can store both the mutex and a reference to the thread.
Keltex
@Keltex. Do I still need to store my singleton in the HttpApplicationState since this is a web app?
Prabhu
@Swami Either there or just as a static class. Both should work.
Keltex