Executing Long-Running Tasks with the Progress Bar in ASP.NET - The structure of the VS.NET solution
(Page 2 of 5 )
The entire VS.NET solution can be freely downloaded from this site to reuse the code in your applications.
Now, what does my VS.NET solution contain? It simply contains three web forms and one class file as follows:
- StartPage.aspx (web form)
- UnderProcess.aspx (web form)
- Finished.aspx (web form)
- ProcessingStatus.vb (Class file)
“StartPage.aspx” contains only a button to start a dummy long running process. The control gets transferred to “UnderProcess.aspx” after the process starts. The “UnderProcess.aspx” shows the progress of achievement in the form of “Percentage complete.” Once the process completes, the control is transferred to “Finished.aspx”, which simply shows the message “Completed Successfully” using a label.
Even though I did not mention “ProcessingStatus.vb” in the above paragraph, it works behind the scenes. It gets involved with both “StartPage.aspx” and “UnderProcess.aspx.” We create a separate thread (for our process) in the “StartPage.aspx” and store information (or status) about the thread using “ProcessingStatus.vb.” This “ProcessingStatus.vb” supplies the information to “UnderProcess.aspx” (whenever requested).
As we start a separate thread at the server, it remains in memory and continuously works with our process (long running task) without having any relationship with “StartPage.aspx” any more. We update the status of thread using “ProcessingStatus.vb” at every milestone of achievement. “UnderProcess.aspx” always gets the information (or status) of the thread from “ProcessingStatus.vb” and displays it visually. Once it receives the information about the completion of thread (indirectly the process), it immediately jumps to “Finished.aspx.”
Starting the Thread
As explained in the previous section, “StartPage.aspx” starts a new thread for a long running process (a dummy task in this scenario). The first important issue is that we need to import “System.Threading” to “StartPage.aspx” because we deal with threads.
I need to provide a unique id for the process, which is going to start within the thread’s context. It should be globally unique, because of the potential issues caused by simultaneous access to the same page by more than one user. I declare and initialize a variable (or object) to hold a globally unique id as follows:
' declaring the Guid
Dim RequestId As Guid
' Create a new request id
RequestId = Guid.NewGuid()
The next step is that we need to start a new thread as follows:
' Create and start a worker thread, to process "something"
Dim ts As New ThreadStart (AddressOf doProcess)
Dim ProcessingThread As New Thread(ts)
ProcessingThread.Start()
What is the funny “doProcess” in the first statement above? It is nothing but our sub-program, which starts our long running process! I separated my entire long process to be under a single sub-program for my convenience (just to attach to the thread).
Response.Redirect("UnderProcess.aspx?RequestId=" + RequestId.ToString())
The above statement redirects to “UnderProcess.aspx” by carrying the globally unique id we created above. This statement gets executed immediately after the “ProcessingThread.Start()” statement.
The beauty of the thread is that it would never stop (or interrupt) the flow of the execution of the program, even if it doesn’t complete the process. It hangs on in memory to complete the process independently by itself, without disturbing the flow of the execution.
All of the above happens when the user simply clicks on the button provided in “StartPage.aspx.”
Next: The long-running process (or task) >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee