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

 

BizTalk first impressions - if you love to code you ain't gonna like it#

however - if you like to solve complex Enterprise Integration problems you'll love the design of this product.

I recently switched jobs and one of the first things I'm doing in my new position is learning BizTalk.  I had no prior experience with it but always had a desire to dig in - I've always heard how it was Microsofts best kept secret.  So last week I was at the Microsoft Technology Center in Waltham for a few days and met with some excellent people who know Biztalk very well.  Their enthusiasm won me over and for the last 3 days I've been reading and watching every whitepaper and webcast I can get my hands on.  I've installed BT2006 on a VPC and run through a couple tutorials and done some virtual labs.  I'm starting to get a handle on where BizTalk fits into solution architecture. 

So, if you love to code and you love Visual Studio you may not like what you see.  Bottom line,  BizTalk does not give the developer a decent environment in which to consume/integrate and debug processing logic.  It's great at forcing an SOA mindset;  creating explicit boundaries, exchanging messages/contracts and not type, etc. (we've all seen the 4 Tenets of SOA slide).  It is great for data transformation tasks and is designed for connecting disparate systems.  But if you need to do more than push and transform data - if you have reams of business rules you need to implement and business users who are not able or willing to maintain them - then you may not be happy with BizTalk's integration with Visual Studio.  The fact that you have to leave Visual Studio to debug an orchestration is not cool.  I really believe that this shortcoming is preventing BizTalk from exploding;  it's still a niche product and it amazes me that Visual Studio support was not a focus for the 2006 release.

The fact is that in most shops developers run the show as far as toolset and implementation... users want quick turnaround and Microsoft has provided the tool of the century to faciliate that.  So until BizTalk joins the RAD party it will always be a struggle for architect types in any shop to get BizTalk adopted.  I've been reading for years that SOA will not take off until toolsets are available to support it.  So Microsoft will soon be shipping WCF and WWF and codehounds will be off to the races.  It's a shame that BizTalk, a product built to faciliate SOA, is not keeping up with the Joneses... whoops I mean Boxes.

I'm sure I'll be posting some more as I continue to learn, it's only been 3 days!, and it may be that I'm missing something... we'll see.

2/3/2006 8:59:30 AM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

Wow... a .Net User Group that's close to home? I never thought it would happen#

Finally there's a user group in west central NJ and the first speaker, Miguel Castro, knows his stuff.  Hopefully we can sustain this group... apparently there are some good people involved (Scott Watermasysk being one of them).  The first meeting is next Thursday February 9th... the topic is ASP.Net 2.0 features - a good place to start.

As an aside I'll mention there's another group that I was involved in, NJEVBUG, that has changed its name to NJDNADUG and has moved its attention toward architecture.  Kent Brown has taken the reigns and has great vision for the group.  Their next meeting is Monday February 13th with the topic being Generics.  Now, I'm not sure if there's an architectural slant to this discussion but I do know Kent is actively polling current members for topic ideas - Enterprise Integration, best practice discussions, patterns, etc... so if you're interested hit the web site and send him some ideas.  NJDNADUG is also an INETA member, meaning they'll get primo speakers a few times a year and free stuff!

And one more....  along the lines of NJDNADUG there is now something called the NYC Connected Systems User Group.  Kent and some other .Net rock stars (Andrew Brust, Stephen Forte, and Bill Zack) got together and formed this group.  Their mission statement says "uniting individuals in the greater NYC area interested in design, implementation, deployment, and support of Enterprise Integration solutions using the Microsoft Connected Systems product stack".  The inaugural meeting for this group is Thursday Feb. 23rd at the NYC Microsoft offices.

Note to wife - will be home late Feb 9th, Feb 13th and Feb 23rd.  Add the NYC Code Camp to that list and February is a busy month!

2/2/2006 10:10:15 AM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

Heartbreak Hotel - VirtualPC, WinFX, Indster and my wireless card#

For 3 hours the other night I beat my head against a wall trying to get VirtualPC to work with my wireless card.  I had some time in the hotel on a trip to Boston and I've been dying to try out a Windows Communication Foundation sample I saw demo'd at the PDC.  Indster is an application written by Omri Gazitt that demonstrates the P2P capabilities of Indigo and has some cool features (uses Amazon's webservices).  I fired up my VPC image and after about an hour had fixed all the breaking changes the RTM bits introduced into Omri's code.  I also had to regenerate the Amazon service proxy but I couldn't get online using my host's wireless nic.  After some time googling I finally found the answer.   Basically the idea is

1-install Microsoft's Loopback Adapter (Add New Hardware wizard)
2-turn on Internet Sharing for your wireless card and point to the Loopback adapter
3-use the Loopback Adapter as your network card in the VPC

I wasted too much time trying to get this to work but finally got it going and got back to checking out Indster.  I'll post a followup with the changes I had to make to get Indster working with VS2005 RTM.

12/5/2005 5:57:20 PM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

WinFX and VS2005 - let the games begin!#

Finally we can use VS2005 to play with Indigo (WCF).  Get the bits here.

11/18/2005 4:05:55 PM (Eastern Standard Time, UTC-05:00) #    Comments [0]  |  Trackback

 

ASP.Net 2.0 - this is a cool feature#
You can take an application offline by dropping a file into the virtual root called App_Offline.htm - aspnet will shut down the app and redirect all incoming requests to this page.  Nice.
10/6/2005 3:33:53 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

NJ Code Camp#

Check it out.  Weekend coding fun.  The NYC version was cancelled but I will not miss this!  Register NOW if you want to go as this will fill up fast.

http://www.njcodecamp.org/

9/27/2005 3:32:12 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

Atlas - Microsoft's implementation of AJAX#

From a DEVX article:

Scott Guthrie, manager of Microsoft's Web Platform and Tools Team, described Atlas' planned features in his blog:

The Atlas Client Script Framework will work on all modern browsers, and with any web server. It also won’t require any client installation at all—to use it, you can simply include references to the right script files in your page.

The Atlas Client Script Framework will include the following components:
  • An extensible core framework that adds features to JavaScript such as lifetime management, inheritance, multicast event handlers, and interfaces
  • A base class library for common features such as rich string manipulation, timers, and running tasks
  • A UI framework for attaching dynamic behaviors to HTML in a cross-browser way
  • A network stack to simplify server connectivity and access to Web services
  • A set of controls for rich UI, such as auto-complete textboxes, popup panels, animation, and drag and drop
  • A browser compatibility layer to address scripting behavior differences between browsers.

Guthrie also says that Microsoft will provide new ASP.NET server controls that you'll be able to use to bind client-side controls to server-side code, and promises simple and direct access to ASMX pages and Indigo services directly through the Atlas Client Script Framework.

While AJAX won't ship with Visual Studio 2005, Microsoft plans to provide it as an add-on layer to ASP.NET and will offer a preview version to developers at the PDC conference in Los Angeles in September.

7/6/2005 3:29:39 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

ASP.Net Version Switcher#

Cool utility that shows all sites configured on a machine and allows you to point to the correct version of the .Net Framework.  Useful if you have the 2.0 beta installed alongside 1.1.  No magic, just a wrapper around aspnet_regiis, but still sweet.

ASP.Net Version Switcher

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

 

VB.Net has jumped the shark#

There are some components shipping with VS 2005, like the one described in this article, that should raise a BIG RED FLAG for every .Net programmer that cares about code.  Something like this should be in the Express edition of VB.Net, not in the Professional edition.  The Express edition is targeted at hobbyists;  I swear anyone that uses this component in an enterprise application should be ashamed of themselves.  I rank this right up there with the ADO data controls in VB6.

As a matter of fact we're adding a new question to the technical screens we give here at XXX Inc.:

XXX Inc.:  "So, have you ever used that cool new multithreaded worker component in Visual Studio 2005?"
Candidate: "Sure, it's so easy to use.  I love it."
XXX Inc.: "Goodbye."

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

 

C# Programming Tools on MSDN#

Check out this list of C# Programming Tools on MSDN....  awesome list thought not necessarily C# specific.  Some of my favorites so far, because I've actually used them, are Reflector and the .Net Memory Profiler... and of course NUnit.

5/27/2005 3:23:03 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

C# Game Development#
There is a series of webcasts available which walk through building a shooter game called Star Trooper.  My son is working his way through this and it's pretty cool.  They take you through the basics of C# while building the game.  For those needing a primer on the basics of C# check this out.  Also all the documentation for the game and the webcasts are available here.
5/27/2005 3:22:13 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

NUnit post build script to copy config file#

Not a big deal but every time I create a new testing assembly I have to search for this stuff.  Put these instructions in the post-build event in project properties for the NUnit assembly.  It will copy the app.config from the project directory into the bin and rename it correctly.   One less manual step we'll have to do when setting up a stream to build and run the test projects.

del $(TargetDir)$(TargetFileName).config
copy ..\..\app.config $(TargetDir)
rename app.config $(TargetFileName).config

5/6/2005 3:20:23 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

Threadsafe counters and lock types#

There's an interesting article in the new MSDN magazine about hyperthreaded CPUs and optimizing code for use with them.  A section of the article talks about synchronization bugs and shows some different locking mechanisms and performance differences between them.  I started with some of the code they supplied, added some other locking mechanisms and hacked up some performance metrics.  The code is attached for those interested.  Some best practices mentioned in the article and things I found when playing around:

1-Avoid using locking in a loop.  This seems obvious but its very easy to fall into this trap if updating some shared counter.  If possible try to increment a local variable in the loop and then lock/update the shared variable once at the end.

2-Use the static members of the Interlocked class instead of the C# lock statement (Monitor) when updating integer/long values.  Interlocked.Increment was 3 times faster than locking on an object.

The code also demonstrates the use of ManualResetEvent and WaitHandle... nothing new to most of you.

The ultimate message of the article... hyperthreaded CPUs give developers more opportunity to look to multithreading as a way to increase throughput and performance.  In many cases using multithreading with single processor machines just gives the perception of increased performance... the CPU can still only do one thing at a time.  Many times multithreading on a single processor machine actually degrades performance because of the context switching that occurs.  Hyperthreading goes one step further and creates 2 logical CPUs per physical CPU;   still not the same thing as a multiprocessor machine but a step that could introduce synchronization bugs that weren't visible before.

I guess the bottom line, as always, is to code defensively.

ThreadSafeCounter.zip (20.04 KB)
5/1/2005 3:16:24 PM (Eastern Daylight Time, UTC-04:00) #    Comments [0]  |  Trackback

 

All content © 2009, Vincent Tripodi