• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Using ASP.NET AJAX to Show Progress on Long Tasks - Part 1 ...

One of the most powerful and compelling reasons to develop something as a application that runs natively on the user’s machine, as opposed to a web application, is responsiveness. The ability to give feedback about an action the user is taking, even if it requires a fair amount of computational time, and even to allow the user to cancel that action is a pretty useful ability. Even as javascript and XML-HTTP have enabled us to craft more responsive user interfaces to the user, the issue of responsiveness to run-longing actions is one that needs to be addressed. Fortunately, those same tools have given us the ability to bridge even that gap.

This series of articles will show how to use ASP.NET AJAX and jQuery to take a long-running task that can be broken up into elements and use that to provide the user with a dialog that provides them feedback on their progress. The particular example used will be running a large number of queries against some slow data source – it is naturally preferable that a faster way of running the queries be found, and the reader is asked to please excuse me for solving this problem and not that one in this series.

The Task

In order to accomplish this, we need to be able to break the task up into chunks. Determining what these chunks are is a task in and of itself. The size of the chunks is important - you’ll be calling them multiple times, so you want to pick a size that is large enough to be meanginful and not be overwhelmed by the overhead of the web service calls you’ll be making, and that is small enough that the interface will still feel responsive. You’ll also need to build a way to determine what the total number of chunks to return are.

Once this is done, wrap this logic in your business layer in whatever way you want to do. In this example, we’ll assume that there is a class like the following:

public interface BusinessObject
{
    // Returns the total number of operations required to
    // finish this task.
    public int GetNumberOfOperations();

    // Performs some of the operations against a set of
    // operations that may be empty. If nothing was done,
    // return false. Otherwise, return true.
    public bool PerformOperation();
}


This code is an example interface, of course, your business object should more closely resemble your model.

Web Service

Having broken up your task, you’ll need to expose those two methods of the interface as a web service. One of the beautifal things about ASP.NET AJAX is that you can easily access web services that have been decorated with the special System.Web.Script.Services.ScriptService attribute through javascript, on the client. I’ll give more details on how to do that in the next article. For the meantime, you’re web session will look something like the following:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class TaskService : System.Web.Services.WebService
{
    public TaskService()
    {

    }

    [WebMethod(EnableSession=true)]
    public int GetNumberOfSteps()
    {
        IBusinessObject obj = (IBusinessObject)Session["businessObject"];
        return obj.GetNumberOfOperations();
    }

    [WebMethod(EnableSession = true)]
    public bool PerformOperation()
    {
        IBusinessObject obj = (IBusinessObject)Session["businessObject"];
        return obj.PerformOperation();
    }
}


You’ll note that each of the methods above are decorated with EnableSession=true. This gives the service access to the same Session state as the rest of your ASP.NET application. In this case, I’m pretending an instance IBusinessObject has been stuffed into the session. In a real world application, you might want to make sure the current user actually has permissions to do the bulky task that they’re about to do.

Caveats

The caveats about this process are, actually, enough to make this only useful in some pretty rarified situations. For the one thing, you don’t actually generally want to do long-running tasks on your web server. Particularly if those long-running tasks are exposed to the outside world and might be called on by every person in the internet. Remember that these tasks, when run, are going to take up one of the worker threads of your ASP.NET pool and that there are a limited number of these. Thus, it is easily possible to take this to a level where your server will collapse underneath the load.

In the end, you might well be better served moving the operation off to another server (perhaps the database) as a scheduled task. You could then use this trick to refresh the status of that operation, instead.

Since I’m trying to solely concentrate on the trick in this series, I’m not going to go into that. But keep in mind the business object could easily get a StartOperation method to add the operation to the queue and a CheckOperation method to see how the operation is going.

Conclusion

I hope this at least whets your appetite. In the next article, I’ll show how to implement this into a web page using javascript and jQuery.

 Original Source:
http://netbard.blogspot.com/2009/07/using-aspnet-ajax-and-web-services-to.html

AddThis Social Bookmark Button

Posted at 08:10:46 am | Permalink | Posted in .Net  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

eBuddy

MSN Web Messenger



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (162)
  • Ajax (82)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (20)
  • Chat (45)
  • ColdFusion (3)
  • CSS (75)
  • Email (23)
  • Facebook (83)
  • Flash (19)
  • Google (54)
  • Html (27)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (54)
  • Javascript (265)
  • jQuery (159)
  • JSON (61)
  • Perl (2)
  • PHP (156)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (29)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (217)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (12)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange