Delegates in 2.0 - and a great article on building a Wait Screen in ASP.NET#

Just about everyone has had to write logic in asp or asp.net to show a page/graphic giving a user something to look at while a webserver is processing some long running task (not unlike reading this sentence).  Daniel Fisher shows how to build a control that does just that.  The client side implementation of this control is sweet... drag it from the toolbox, set a property for the image location and hook up your event;  that's it.  He takes you step by step through the process, including implementing design time support.  Check it out, very cool.

In Daniel's sample the delegate is hooked in the aspx declaration for the control.  You may want to programatically hook it.  The code for programatically hooking events got me thinking about the changes for delegates in 2.0... I modified the code for 2.0 - taking advantage of anonymous delegates and the shortened syntax for delegate assignment.

From the client you can hook the delegate like this in 2.0

   this
.WaitScreen1.Process += WaitScreen1_Process;    

where in 1.x you had to do:

   this
.WaitScreen1.Process +=new EventHandler(WaitScreen1_Process);

In the control it gets easier also.   In Daniel's code he simply fires the delegate (and for good reason, his sample was a demonstration of control building, not delegate best practice).  The usual pattern for firing a delegate is to check for null beforehand.  Also, there are race conditions to consider because in between checking the delegate for null and firing it, the delegate could become null.  So if you want to optionally hook the delegate in the sample you'll need to add the null check to the code for the control.  Or not!  Enter anonymous delegates.  In 2.0 you can get away with the following.  Notice the use of an anonymous delegate (which does nothing) as a default assignment to the event.

public
event EventHandler Process = delegate { };

public virtual void OnProcess() {
   P
rocess(this, null
);
}

So, Process will never be null;  worst case scenario you've got an empty method executing.  I did some quick Stopwatch performance analysis on this method vs. null checking and locking - the difference was almost nil.  25 ticks vs. 22 ticks on average (fractions of a millisecond).

I said earlier "you can get away with" the above code... best practice guidelines say your event declaration should be private with a public event handler.  Also, it's a good idea for the method which does the firing to get the InvocationList and iterate through each in a try-catch block.   You are executing untrusted code afterall, in the sense that the target method may throw an exception and the rest of the delegate instances will not execute.  One of my .Net heroes, Juval Lowy, covers this in his outstanding book Programming .Net Components

private event EventHandler _process = delegate { };

public
event EventHandler
Process {
   add
{
      _process += value;
   }
   
remove
{
      _process -= value;
   }
}

public virtual void OnProcess() {
   Delegate
[] clients;

   clients = _process.GetInvocationList();

   
foreach (EventHandler client in
clients) {
      try
{
         client(
this, EventArgs
.Empty);
      }
      catch
{
         
_process -= client;
      }
   }
}

Locking is recommended when publishing events in a remoting scenario, both when getting the Invocation List (and also in the event procedure when hooking and unhooking).  Mike Woodring explains this here and has some other great tips on his site as well.

2/24/2006 3:37:56 PM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

XSL 1.0 Split function#

Been spending a little more time than I'd like with XSLT.  I recently needed a split function that would recursively create elements based on a delimited string.  Well, although XSLT 2.0 appears to have support for split, 1.0 does not.  So here goes for anyone who might need it.

<xsl:template match="some_delimited_string">
   <xsl:if test="normalize-space(.)">
      <xsl:call-template name="split">
         <xsl:with-param name="text" select="."/>
         <xsl:with-param name="element">YourElementName</xsl:with-param>
         <xsl:with-param name="delim">/</xsl:with-param>
      </xsl:call-template>
   </xsl:if>
</xsl:template>

<xsl:template name="split">
   <xsl:param name="text" select=""/>
   <xsl:param name="element" select=""/>
   <xsl:param name="delim" select=""/>
   <xsl:choose>
      <xsl:when test="contains($text, $delim)">
         <xsl:element name="{$element}">
            <xsl:value-of select="substring-before($text, $delim)"/>
         </xsl:element>
         <xsl:call-template name="split">
            <xsl:with-param name="text" select="substring-after($text, $delim)"/>
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="delim" select="$delim"/>
         </xsl:call-template>
      </xsl:when>
   <xsl:otherwise>
      <xsl:element name="{$element}">
         <xsl:value-of select="$text"/>
      </xsl:element>
   </xsl:otherwise>
   </xsl:choose>
</xsl:template>

2/21/2006 5:00:32 PM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

Project Aardvark #

Add another item to the list of "Damn, I wish I thought of that".  Joel Spolsky has created a documentary about a software development project.  He interviewed hundreds of interns and came up with 4, who were given 12 weeks to design, develop, test and ship a product.  This looks to me like a horror film, since after I watch it I'll realize these interns could probably kick my geek ass.  Hopefully I can put my ego aside;  I may just learn something.

Check out the trailer for Project Aardvark.  It looks GREAT.  Something tells me this might just make it to IFC and get some exposure;  it looks that good.

Copilot is the product they built - looks similiar to GotoMyPC.com

11/8/2005 1:39:31 PM (Eastern Standard Time, UTC-05:00) #    Comments [2]  |  Trackback

 

Who says you can't take it with you?#

I am an infant.  I saw some of my coworkers showing off their versions of this 21st century briefcase and had to have one.  My data, code, music, VS2005 vpc images will now travel with me.  Thanks to Kevin and Don for pointing this out and Miguel for hints on the model to get.

Drive
Case
                   

10/21/2005 11:26:51 AM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

FollowUp URLs - This is cool#

So lets say you're surfing and you come across a link you want to re-visit when you have time... so you right click on the link and there's an IE context menu option that allows you to create an Outlook Task so you can follow up on the link at a later time.   Up pops the Create New Task dialog in Outlook and you save that link for followup at a later time.

Get it here:

http://philiprieck.com/blog/archive/2004/05/11/FollowupURL.aspx

5/25/2005 3:21:15 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

Factory Design Pattern & PatternShare.org#

As Anna correctly pointed out the abstract factory implementation would use an interface so that the client does not directly reference the concrete class.

This is a great wiki with pattern overviews:
http://patternshare.org/default.aspx/Home.HomePage

This is an even better site showing UML and implementation:
http://www.dofactory.com/Patterns/Patterns.aspx

3/28/2005 4:00:19 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

All content © 2009, Vincent Tripodi
On this page
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Blogroll OPML
Disclaimer

All content on this blog has somehow touched the mind of a desperate individual looking for attention from anywhere he can get it.  Do not hold anyone responsible for what appears here except the author.  Sue me, not my employer.  I welcome the attention.

Send mail to the author(s) Vincent Tripodi