ASP.NET WaitPage Framework (4/5)
[Note: Code is for ASP.NET 1.1, but converts to ASP.NET 2.0]
Download code and reference documentation for this article:
View WaitPage Reference Documentation
3. Quick Guide To WaitPage Framework Usage
This section is intended as a quick summary of how to set up an app to use the WaitPage
Framework, and is also for those who want to use the framework without necessarily
wading through all the detailed explanation above.
< prev 1 | 2 | 3 | 4 | 5 next >
- Derive a class from BaseTaskWaiter (This is referred to as TaskWaiter below)
In this class:
- Write a function called StartTask to actually start your long running task. Have
the parameters for the long running task passed in the class’s constructor.
- Override IsTaskFinished to query the long running task, and return true if the
task has finished, and false otherwise.
- Override GetResults to return the results from the long running task. It’s good
practice to throw an exception or return some kind of special value for the result,
such as null, if the task hasn’t finished yet.
- Create a WaitPage Web Form. (WaitPage.aspx in the app’s root is the default URL
expected by the framework. If you need a different one, see below in the ‘Other
- Add at least a Label to the webform, to display the ‘waiting’ message.
- Make the WaitPage webform implement IWaitPage, which consists of the function
SetWaitingMessage. Typical implementation is as follows:
public void SetWaitingMessage(string waitMessage)
lblWaitMessage.Text = waitMessage;
In the WaitPage’s PageLoad call WaitPageHelper.DoWait(this). Be aware that this can return a WaitPageInvalidContextException.
In another Web Form, add an event handler for the action which starts the long running task, typically a button handler.
In this event handler, you need to:
Construct an instance of the TaskWaiter class created above
Register the object with the WaitPageHelper, by calling RegisterTaskWaiter. Also pass in the Result URL, which will be redirected to when the long running task completes.
If the result page URL is the same page as that which has this event handler, then subscribe to the NotifyResultsStatus event in the TaskWaiter, after creation
Start the task by calling the TaskWaiter’s StartTask function.
If it starts OK, call WaitPageHelper.StartWaiting().
If necessary, subscribe to the NotifyResultsStatus event in the TaskWaiter class, and write the handler. The handler should normally be static, and the bResultsReady argument should be stored in a session variable.
In the Result page, if this is the same as the page starting the task:
check the session variable used in Step 4 to see if results are ready, in Page_Load
In all cases, in the Result page, get the results from the WaitPageHelper, cast to appropriate type and display or otherwise use.