<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DotBert &#187; delegate</title>
	<atom:link href="http://dotbert.loedeman.nl/category/delegate/feed" rel="self" type="application/rss+xml" />
	<link>http://dotbert.loedeman.nl</link>
	<description>.NET thoughts by Bert Loedeman</description>
	<lastBuildDate>Wed, 19 Oct 2011 07:57:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Multicast delegate invocation</title>
		<link>http://dotbert.loedeman.nl/multicast-delegate-invocation</link>
		<comments>http://dotbert.loedeman.nl/multicast-delegate-invocation#comments</comments>
		<pubDate>Fri, 10 Oct 2008 12:40:00 +0000</pubDate>
		<dc:creator>Bert Loedeman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[event invocation]]></category>
		<category><![CDATA[getinvocationlist]]></category>
		<category><![CDATA[multicast delegate]]></category>

		<guid isPermaLink="false">http://www.loedeman.net/wordpress/?p=21</guid>
		<description><![CDATA[Today I remembered the need to blog again. It sounds weird, but it isn&#8217;t &#8230; A while ago I ran into a failing multicast delegate and decided to blog it. Since I ran into one again today and having to look up my previous solution again the hard way, I discovered I should have done [...]]]></description>
			<content:encoded><![CDATA[<p>Today I remembered the need to blog again. It sounds weird, but it isn&#8217;t <img src='http://dotbert.loedeman.nl/wp-includes/images/smilies/icon_smile.gif' alt="Multicast delegate invocation icon smile" class='wp-smiley' title="Multicast delegate invocation" />  &#8230; A while ago I ran into a failing multicast delegate and decided to blog it. Since I ran into one again today and having to look up my previous solution again the hard way, I discovered I should have done it immediately. Check the story below:</p>
<p>Multicast delegates are great. Today I had to implement a custom user control (insurance cost calculation) with several child controls (one for each prospect) which depend on each other (e.g. parent and children). I use a delegate to notify the parent control that one of it&#8217;s children selections has changed.</p>
<p>The original call:</p>
<pre class="brush: csharp; title: ; notranslate">
public event EventHandler SelectionChanged;

/// &lt;summary&gt;
/// This method cares for correct event handling
/// &lt;/summary&gt;
private void OnSelectionChanged()
{
  // The SelectionChanged event will be null if there are no subscribers.
  if (SelectionChanged != null) SelectionChanged(this, EventArgs.Empty);
}
</pre>
<p>For some dumb reason I ran into a NRE using the delegate. Guess what happened? Subsequent delegate subscribers did not get fired anymore. The reason is that a delegate that fails for some reason, fails entirely. After researching the problem again, I changed the code provided hereabove with the following piece of code:</p>
<pre class="brush: csharp; title: ; notranslate">
public event EventHandler SelectionChanged;

/// &lt;summary&gt;
/// This method cares for correct event handling, including the protection against
/// failing individual event subscribers
/// &lt;/summary&gt;
private void OnSelectionChanged()
{
  // The SelectionChanged event will be null if there are no subscribers.
  if (SelectionChanged != null)
  {
    try
    {
      Delegate[] list = SelectionChanged.GetInvocationList();
      foreach (Delegate del in list)
      {
        try
        {
          // Only the current subscriber will file, the rest of the delegate's
          // invocation will proceed as normal.
          EventHandler handler = (EventHandler) del;
          handler(this, EventArgs.Empty);
        }
        catch { }
      }
    }
    catch { }
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotbert.loedeman.nl/multicast-delegate-invocation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

