SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

Handle Your Events - Perform Better

(Apr 24 2007 - 09:56:03 PM by Timothy Khouri) - [print article]

A Quick Overview

If you've ever looked into way Controls are done in ASP.NET by the actual .NET team you'll see some smart performance boosts that us foolish mortals (people not on the .NET team) may not be aware of. One of those techniques is to do your own "Event Accessors" which can really help keep your memory usage down on your web applications. There are a lot of benefits of doing this very simple thing.

If you are making your own custom control or even a custom class such as a BLL (Business Logic Layer) or DAL (Data Access Layer) and you are going to make your own custom events then there are a few things you are going to want to know. First of all, every event that you make is going to create a EventHandlerList behind the scenes to add and remove delegates that people create when they register with your event with a += (or -= to unregister).

What Does That Mean?

There isn't a huge issue here, but if you have multiple events, then you're eating up more and more ram with each EventHandlerList object that is created. Every web control that I have looked into in the .NET Framework (meaning the GridView, Page, TextBox, DropDownList, etc.) all handle their own event accessors by calling the add and remove code themselves. They utilize the "Events" property which is on all objects that inherit from the base "Control" class (which you would too if you were making your own custom control).

You can utilize the "Events" property if you inherit from Control by doing the following:

public class MyCustomControl : Control
{
   public event EventHandler MyCustomEvent
   {
       add
       {
           // Because AddHandler needs an object for the key,

           // I am using the name of the event as a string. This

           // is an easy way of handling it.

           base.Events.AddHandler("MyCustomEvent", value);
       }

       remove
       {
           base.Events.RemoveHandler("MyCustomEvent", value);
       }
   }
}

So a very small amount of code can really improve the performance of your custom controls. But what if you wanted to do this with a custom class that isn't inheriting from "Control" or "WebControl" such as a BLL, DAL, Subject Layer or whatever? Well, thankfully that's pretty simple too. The EventHandlerList class isn't tied to ASP.NET controls only. In fact, it's part of the System.ComponentModel namespace, meaning it can be used anywhere (including Windows or Console applications). So if we make a slight modification to the code above by creating our own EventHandlerList instance, we can use it in just the same way that we would if we were making a custom control.

using System.ComponentModel;

public class MyCustomClass
{
   private EventHandlerList Events = new EventHandlerList();

   public event EventHandler MyCustomEvent
   {
       add
       {
           this.Events.AddHandler("MyCustomEvent", value);
       }

       remove
       {
           this.Events.RemoveHandler("MyCustomEvent", value);
       }
   }
}

You can even go one step further than this if you really want to by making your event object static and using a private instance object as the key for the AddHandler and RemoveHandler. This way if you have 100 number people hitting your web application you only won't have 100 EventHandlerLists being created and destroyed all the time. Here is how you would do that:

using System.ComponentModel;

public class MyCustomClass
{
   private static EventHandlerList Events = new EventHandlerList();

   private object myCustomEventKeyObject;

   public event EventHandler MyCustomEvent
   {
       add
       {
           lock (MyCustomClass.Events)
           {
               MyCustomClass.Events.AddHandler(this.myCustomEventKeyObject, value);
           }
       }

       remove
       {
           lock (MyCustomClass.Events)
           {
               MyCustomClass.Events.RemoveHandler(this.myCustomEventKeyObject, value);
           }
       }
   }
}

That's pretty much it. There are many more ways to improve the performance of your web applications beyond this which I will go into eventually. This one in particular used to only work in C#, but now even VB.NET can create event accessors.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Check Out Dev++

Test your development skills, give proof to recruiters and employers at dev++

Related Blogs

These are the most recent blog posts related to this article.

  • Aspose.Newsletter February 2012 Edition is out Now
  • Embed Videos inside PowerPoint Presentation & Stable PDF Generation
  • Excel to PDF Conversion, XLS, XLSX & ODS Rendering are Improved
  • Convert SVG to Pdf & Render Pdf into Image with Desired Dimensions
  • Add Optical Character Recognition in Java Web & Desktop Applications

Related Ads

SingingEels.com as of Feb 04 2012 - 12:39:30 AM - (0.0781245)