Executing Long-Running Tasks with the Progress Bar in ASP.NET - The long-running process (or task)
(Page 3 of 5 )
For the long running process, I could not find a proper example other than providing a loop with a certain amount of delay. The loop is iterated ten times and is delayed for about three seconds on every iteration. In that way, I made a dummy long-running process, which takes about thirty seconds to complete.
I don’t think that the long-running process is the main issue here in this article. The most important issue is to update the thread information using “ProcessingStatus.vb” and to show it visually with “UnderProcess.aspx.” The following is the code fragment which is used as the long-running task in this article:
'This method is executed by the processing thread.
Private Sub doProcess()
'do some long task processing here
ProcessingStatus.add(RequestId, 0)
'start with percentage 0
Dim i As Integer
For i = 1 To 10
Thread.Sleep(New TimeSpan(0, 0, 0, 3,0))
'wait for 3 seconds for every iteration
ProcessingStatus.update(RequestId, i * 10)
'update percentage with a value between 0 to 100
Next
ProcessingStatus.update(RequestId, -1)
'after completing the task succesfully, just update status as -1
End Sub
I provided commenting everywhere necessary. The first important issue to understand is that the above sub-program (doProcess) gets executed in a separate thread (as explained in the previous section). Let us consider the first statement within the above fragment:
ProcessingStatus.add(RequestId, 0)
The above statement adds new GUID with percentage completion as zero using “ProcessingStatus.vb.” This gets executed when a new process is about to start (taking into the scenario of multiple users).
Thread.Sleep(New TimeSpan(0, 0, 0, 3, 0))
The above statement creates a delay of three seconds.
ProcessingStatus.update(RequestId, i * 10)
The above statement updates the status of GUID added above with a certain percentage value for every iteration of the loop.
ProcessingStatus.update(RequestId, -1)
The above statement updates the percentage of same GUID added above with a percentage value of “-1”. I made a rule in my application that, if the percentage value is “-1”, the process has been successfully completed (this would in turn make “UnderProcess.vb” understand easily that the process has been completed).
Next: Showing the progress >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee