Giving feedback during a long running task
Some tasks take a long time, there is no avoiding it.
Some factors are under your control and can be optimised, such as your data caching. Others are not, for example external services.
When considering what if any feedback to give to the users, you need to consider the following:
- What is the most time the user should go without any feedback?
- What happens if the user is left without feedback for too long and tries to refresh the page?
- Can the task be broken down into discrete steps?
- Can the steps run in parallel or do they need to execute in a sequence?
- Do all steps need to complete?
A lot of web transactions are simple one step transactions. The user posts data to the web server, it performs a simple operation then redirects the user to another page which shows the result.
This pattern is described here: http://en.wikipedia.org/wiki/Post/Redirect/Get
If the task takes a short amount of time (e.g. 10 seconds), then a wait cursor or ‘Please wait’ message would be the simplest feedback you could do.
Note: duplicate transactions are possible if the user re-posts the page before processing completes. This scenario must therefore be handled with code on the server.
Click to see a Demo: /mvc/Feedback/PostRedirectGet
A long running task could be one that takes more than 10 seconds. The task could run in the background on the server and the user be redirected to a ‘holding’ page.
When the task is running the user would like to see feedback.
To give the user this feedback the client must either pull or server must push this information. This should happen every few seconds until the task completes.
This could be described as a Post / Redirect / Get / Redirect / Get pattern.
A simple example of the client pulling the data would be to reload the page from the server every few seconds. When the background task was complete, there would be a redirect to the results page.
Click to see a Demo: /mvc/Feedback/PostRedirectGetRedirectGet
A server push solution could use Signal R, which allows the server to push progress data to the client to display to the user.
Click to see a Demo: /mvc/Feedback/SignalR
Real World Example
‘comparethemarket.com’ is a comparison site which provides car insurance quotes.
After you request a quote, a list is built on the page while you wait. After about 45 seconds the search is stopped and the results are shown.
1. Block the page displaying ‘Searching…’
2. Post the request for quotes to the server.
3. Request new quotes from the server in JSON.
4. If the server provides quotes they are added to the page and the search feedback is updated.
5. If the response Indicates that the search has not finished then sleep for 5 seconds then go back to step 3.
6. Unblock the page.