<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>The Brain Dump</title><link>http://blogs.msdn.com/b/benwilli/</link><description>My brain poured out on paper (or so to speak)</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/msdn/BrainDump" /><feedburner:info uri="msdn/braindump" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>32.876474</geo:lat><geo:long>-96.941298</geo:long><item><title>Tasks are not Threads</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/dmSBo7oEdt0/tasks-are-not-threads.aspx</link><pubDate>Wed, 24 Apr 2013 14:16:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10413654</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10413654</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10413654</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2013/04/24/tasks-are-not-threads.aspx#comments</comments><description>&lt;p&gt;One of the common misconceptions I’ve encountered when developers first start using the Task Parallel Library is that they think Tasks are just fancy threads. This is easy to assume because in a common case, calling Task.Run(…), it actually does run the Task activity on a thread from the thread pool. But as &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/02/11/10266920.aspx"&gt;Stephen Toub wrote&lt;/a&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The &lt;a href="http://go.microsoft.com/fwlink/?LinkId=204847"&gt;Task-based Async Pattern&lt;/a&gt; (TAP) isn’t just about asynchronous operations that you initiate and then asynchronously wait for to complete.&amp;#160; More generally, tasks can be used to represent all sorts of happenings, enabling you to await for any matter of condition to occur. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;For example, let’s say I have a WPF app with two buttons, Start and End. The Start button’s event handler looks like this&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;   &lt;div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;     &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum1" style="color: rgb(96, 96, 96);"&gt;   1:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; async &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; bttnStart_Click(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum2" style="color: rgb(96, 96, 96);"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum3" style="color: rgb(96, 96, 96);"&gt;   3:&lt;/span&gt;     MessageBox.Show(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Looking for the time&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum4" style="color: rgb(96, 96, 96);"&gt;   4:&lt;/span&gt;     DateTime now = await GetCurrentTimeAsync();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum5" style="color: rgb(96, 96, 96);"&gt;   5:&lt;/span&gt;     MessageBox.Show(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;It is now&amp;quot;&lt;/span&gt; + now.ToString());&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum6" style="color: rgb(96, 96, 96);"&gt;   6:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;which uses an async method to retrieve the current time. But we can implement GetCurrentTimeAsync() this like the following&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.47%; height: 253px; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;div id="codeSnippet" style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;
    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum1" style="color: rgb(96, 96, 96);"&gt;   1:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; bttnEnd_Click(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum2" style="color: rgb(96, 96, 96);"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum3" style="color: rgb(96, 96, 96);"&gt;   3:&lt;/span&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_tsc != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum4" style="color: rgb(96, 96, 96);"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum5" style="color: rgb(96, 96, 96);"&gt;   5:&lt;/span&gt;         _tsc.SetResult(DateTime.Now);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum6" style="color: rgb(96, 96, 96);"&gt;   6:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum7" style="color: rgb(96, 96, 96);"&gt;   7:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum8" style="color: rgb(96, 96, 96);"&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum9" style="color: rgb(96, 96, 96);"&gt;   9:&lt;/span&gt; TaskCompletionSource&amp;lt;DateTime&amp;gt; _tsc;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum10" style="color: rgb(96, 96, 96);"&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum11" style="color: rgb(96, 96, 96);"&gt;  11:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; Task&amp;lt;DateTime&amp;gt; GetCurrentTimeAsync()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum12" style="color: rgb(96, 96, 96);"&gt;  12:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum13" style="color: rgb(96, 96, 96);"&gt;  13:&lt;/span&gt;     _tsc = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;DateTime&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span id="lnum14" style="color: rgb(96, 96, 96);"&gt;  14:&lt;/span&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _tsc.Task;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"&gt;&lt;span id="lnum15" style="color: rgb(96, 96, 96);"&gt;  15:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The Task returned from GetCurrentTimeAsync() will never complete until the user clicks the End button. In this case, the Task activity isn't code, but the act of waiting for the user to click the End button. In real code the activity could be happening on the network, the memory bus of the machine or anywhere, but it isn't necessarily on a thread.&lt;/p&gt;

&lt;p&gt;In fact, Lucien Wischik has an &lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;excellent series&lt;/a&gt; on turning all sorts of things into Tasks so that they can be awaited.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10413654" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/dmSBo7oEdt0" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/TPL/">TPL</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2013/04/24/tasks-are-not-threads.aspx</feedburner:origLink></item><item><title>Windows 8 ViewModel Property Code Snippet</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/60HhPhO69oc/windows-8-viewmodel-property-code-snippet.aspx</link><pubDate>Fri, 12 Oct 2012 20:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10359244</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10359244</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10359244</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2012/10/12/windows-8-viewmodel-property-code-snippet.aspx#comments</comments><description>&lt;p&gt;Visual Studio provides a bunch of good code snippets for creating boilerplate code. For example if you type &amp;ldquo;prop&amp;rdquo; you get this in intellisense&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/1817.image_5F00_419283A4.png"&gt;&lt;img width="344" height="239" title="image" style="display: inline; background-image: none;" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/8345.image_5F00_thumb_5F00_47D95A32.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll see a few nice property snippets.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;prop&lt;/strong&gt; &amp;ndash; creates a basic automatic property&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;propa&lt;/strong&gt; &amp;ndash; creates an attached property&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;propdp&lt;/strong&gt; &amp;ndash; creates a dependency property&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;propfull&lt;/strong&gt; &amp;ndash; creates a public property with a private backing field&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;propg&lt;/strong&gt; &amp;ndash; creates a basic automatic property but the set is marked private&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I use these constantly but hit an issue when building my Windows 8 apps. For ViewModels in Windows 8, the default templates take advantage of a wonderful attribute CallerMemberName in the OnPropertyChanged method of the ViewModel base class. This allows you to create properties that support notifications in a very simple manner. Like this&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/3652.image_5F00_522A7E92.png"&gt;&lt;img width="415" height="119" title="image" style="display: inline; background-image: none;" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/7752.image_5F00_thumb_5F00_58DD8815.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But that pattern doesn&amp;rsquo;t have code snippet. That&amp;rsquo;s easy to fix though. I created my own snippet to generate that type of property and named it proprt.&lt;/p&gt;
&lt;p&gt;Just create a file called &lt;strong&gt;proprt.snippet &lt;/strong&gt;in the folder &lt;strong&gt;[USERPROFILE]\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It should have the following contents&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippets&lt;/span&gt;  &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt; &lt;span class="attr"&gt;Format&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0.0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;proprt&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;proprt&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Code snippet for property and backing field with change notification in Windows Runtime view models&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Microsoft Corporation&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;type&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Property type&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;int&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;property&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Property name&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;MyProperty&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;field&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The variable backing this property&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;myVar&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt; &lt;span class="attr"&gt;Language&lt;/span&gt;&lt;span class="kwrd"&gt;="csharp"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;[CDATA[private $type$ $field$;

    public $type$ $property$
    {
        get { return $field$;}
        set { this.SetProperty(ref this.$field$, value); }
    }
    $end$]]&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;CodeSnippets&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then you should see it show up in the Visual Studio Code Snippet Manager under My Code Snippets&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/3051.image_5F00_111BFF2E.png"&gt;&lt;img width="473" height="354" title="image" style="display: inline; background-image: none;" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/1411.image_5F00_thumb_5F00_7747C8FE.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Voila, start creating properties with it and build your Windows 8 Store apps even easier!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10359244" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/60HhPhO69oc" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/Visual+Studio/">Visual Studio</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/tips/">tips</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2012/10/12/windows-8-viewmodel-property-code-snippet.aspx</feedburner:origLink></item><item><title>Windows Phone MessageBox implements scrolling!</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/NRJrd7-x5Gw/windows-phone-messagebox-implements-scrolling.aspx</link><pubDate>Mon, 06 Aug 2012 20:27:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10337288</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10337288</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10337288</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2012/08/06/windows-phone-messagebox-implements-scrolling.aspx#comments</comments><description>&lt;p&gt;Helpful quick and dirty debugging tip: I never noticed this before, but if you send a large amount of text to MessageBox.Show() on Windows Phone, it doesn’t just truncate it. It allows you to scroll through it all. Very cool. Very &lt;em&gt;unlike&lt;/em&gt; the desktop message box.&lt;/p&gt;  &lt;p&gt;I was tinkering today with the web browser control and manipulating the HTML contents via InvokeScript and needed a way to verify the changes. What I was changing was unfortunately not at the very top of the HTML, but I discovered I could easily dump the contents like this&lt;/p&gt;  &lt;pre&gt;MessageBox.Show(webBrowser1.SaveToString());
&lt;/pre&gt;

&lt;p&gt;and then just scroll down to the portion I was trying to verify had changed.&lt;/p&gt;

&lt;p&gt;Now, I’m not recommending that you dump large text to the message box for a user to scroll through – that would be poor form. But as a quick diagnostic trick it can be handy.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10337288" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/NRJrd7-x5Gw" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7Dev/">WP7Dev</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/tips/">tips</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2012/08/06/windows-phone-messagebox-implements-scrolling.aspx</feedburner:origLink></item><item><title>Managing multiple web service calls with Rx</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/PlkWsYKgGPQ/managing-multiple-web-service-calls-with-rx.aspx</link><pubDate>Fri, 15 Jun 2012 21:33:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10320742</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10320742</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10320742</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2012/06/15/managing-multiple-web-service-calls-with-rx.aspx#comments</comments><description>&lt;p&gt;Lets say I have a phone app (or any app really) that needs to make several web service calls at once. For example, imagine the user multiselects three items on a page and then taps the “add favorite” button. I then need to make three calls to the “set favorite” service. I’ll do this asynchronously so that they can all run simultaneously, but if I make three asynchronous calls to a web API that updates a local collection of resources, I want to be able to&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;know which ones failed, to notify the user &lt;/li&gt;    &lt;li&gt;know which ones succeeded, to update the local data model &lt;/li&gt;    &lt;li&gt;get a notification when all of them are complete so I can turn off the “Is Busy” indicator in the UI &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Typically the first two are no problem because I could run them all through the same logic each having its own completion callback which does the right thing. The trick comes in aggregating the responses all together so that I can do #3 – turn off the progress control. Normally this would entail creating some state object and tracking how many items were started, identifying them as they finished and updating the state object all in a thread safe manner. This is also complicated by the fact that the requests may (and likely will) complete in a different order from that which I started them.&lt;/p&gt;  &lt;p&gt;Because I like to &lt;a href="http://blogs.msdn.com/b/benwilli/archive/2011/11/01/populating-windows-phone-view-models-with-rx.aspx"&gt;use Rx to populate my viewmodels,&lt;/a&gt; my web requests are IObservables which means that I can compose them in a way to get the behavior I want without worrying about threading or state management.&lt;/p&gt;  &lt;p&gt;Lets say I’m making a call to mark something as a favorite. The service proxy looks like this. On success, the service will echo the new value sent to it.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IObservable&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; SetFavorite(&lt;span class="kwrd"&gt;int&lt;/span&gt; ID, &lt;span class="kwrd"&gt;bool&lt;/span&gt; isFavorite)&lt;/pre&gt;


&lt;p&gt;A single call would look something like this&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SingleCall()
{
    FavoritesService svc = &lt;span class="kwrd"&gt;new&lt;/span&gt; FavoritesService();
    var svcCall = svc.SetFavorite(5, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

    svcCall.ObserveOnDispatcher().Subscribe(
        result =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call succeeded = {0}&amp;quot;&lt;/span&gt;, result),
        ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call failed: {0}&amp;quot;&lt;/span&gt;, ex),
        () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call completed&amp;quot;&lt;/span&gt;)
    );
}
&lt;/pre&gt;


&lt;p&gt;If I needed to favorite three items I just do it three times.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; IndividualCalls()
{
    FavoritesService svc = &lt;span class="kwrd"&gt;new&lt;/span&gt; FavoritesService();

    var call1 = svc.SetFavorite(5, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
    var call2 = svc.SetFavorite(6, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
    var call3 = svc.SetFavorite(7, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

    call1.ObserveOnDispatcher().Subscribe(
        result =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call1 succeeded = {0}&amp;quot;&lt;/span&gt;, result),
        ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call1 failed: {0}&amp;quot;&lt;/span&gt;, ex),
        () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call1 completed&amp;quot;&lt;/span&gt;)
    );

    call2.ObserveOnDispatcher().Subscribe(
        result =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call2 succeeded = {0}&amp;quot;&lt;/span&gt;, result),
        ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call2 failed: {0}&amp;quot;&lt;/span&gt;, ex),
        () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call2 completed&amp;quot;&lt;/span&gt;)
    );

    call3.ObserveOnDispatcher().Subscribe(
        result =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call3 succeeded = {0}&amp;quot;&lt;/span&gt;, result),
        ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call3 failed: {0}&amp;quot;&lt;/span&gt;, ex),
        () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Call3 completed&amp;quot;&lt;/span&gt;)
    );
}
  &lt;/pre&gt;

&lt;p&gt;But how do I know when &lt;em&gt;all three &lt;/em&gt;are done? My first thought was to just use Merge&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MergedCalls()
{
    FavoritesService svc = &lt;span class="kwrd"&gt;new&lt;/span&gt; FavoritesService();

    var call1 = svc.SetFavorite(5, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
    var call2 = svc.SetFavorite(6, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
    var call3 = svc.SetFavorite(7, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

    var allCalls = call1.Merge(call2).Merge(call3);

    allCalls.ObserveOnDispatcher().Subscribe(
        result =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;All calls succeeded = {0}&amp;quot;&lt;/span&gt;,&lt;br /&gt;                                    result),
        ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;Some calls failed: {0}&amp;quot;&lt;/span&gt;, ex),
        () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;All calls completed&amp;quot;&lt;/span&gt;)
    );
}&lt;/pre&gt;

&lt;p&gt;But there is a major issue with this. If an exception happens in an observable sequence, the sequence is over. So if any of the calls throw a fault (e.g. if there is a network error) then I wont receive any notifications for any of the calls that haven't yet completed. I tried a number of variations on this until &lt;a href="http://channel9.msdn.com/tags/Bart+de+Smet/"&gt;Bart De Smet&lt;/a&gt; pointed out the &lt;a href="http://msdn.microsoft.com/en-us/library/hh229453(v=VS.103).aspx"&gt;Materialize operator&lt;/a&gt; to me. &lt;/p&gt;

&lt;p&gt;Materialize “materializes the implicit notifications of an observable sequence as explicit notification values.” What this means is that if you materialize a sequence of strings you get a sequence of &lt;a href="http://msdn.microsoft.com/en-us/library/hh229462(v=vs.103).aspx"&gt;Notification&amp;lt;string&amp;gt;&lt;/a&gt;. The Notification&amp;lt;string&amp;gt; object tells you if it was a string value, exception, or completion. Therefore, you get the information but the sequence doesn’t throw errors. Instead, the error becomes a value in the sequence.&lt;/p&gt;

&lt;p&gt;I can now materialize each of the three calls and then merge them together. If I generalize this to N calls to my SetFavorites it looks something like this&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetFavoritesWithMaterialize2(List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; IDs)
{
    FavoritesService svc = &lt;span class="kwrd"&gt;new&lt;/span&gt; FavoritesService();
    var total = IDs.ToObservable()
                    .SelectMany(id =&amp;gt; &lt;br /&gt;                         svc.SetFavorite(id, id % 2 == 0 ? &lt;span class="kwrd"&gt;true&lt;/span&gt; : &lt;span class="kwrd"&gt;false&lt;/span&gt;)
                            .Materialize()
                            .Select(callResult =&amp;gt; &lt;br /&gt;                                     &lt;span class="kwrd"&gt;new&lt;/span&gt; { Result = callResult, ID = id })
                     );
    total.Subscribe(call =&amp;gt;
            {
                &lt;span class="kwrd"&gt;switch&lt;/span&gt; (call.Result.Kind)
                {
                    &lt;span class="kwrd"&gt;case&lt;/span&gt; NotificationKind.OnNext:
                        Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;CLIENT {0} succeeded = {1}&amp;quot;&lt;/span&gt;, &lt;br /&gt;                                           call.ID, call.Result.Value);
                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;case&lt;/span&gt; NotificationKind.OnError:
                        Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;CLIENT {0} failed&amp;quot;&lt;/span&gt;, call.ID);
                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;
                }
            },
            ex =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;CLIENT Something failed: {0}&amp;quot;&lt;/span&gt;, ex.Message),
            () =&amp;gt; Debug.WriteLine(&lt;span class="str"&gt;&amp;quot;CLIENT All completed&amp;quot;&lt;/span&gt;)
        );
}
&lt;/pre&gt;


&lt;p&gt;Initially I was foreach’ing over the list of IDs, but ‘duh! it’s a sequence!’, turn it into an observable and pipe them in the SetFavorite call. Then materialized the values and inject the current ID of the call with a Select operator so that later in the Subscribe method I can determine which call is succeeding or failing. The resulting sequence is a sequence of IObservables so I used SelectMany to flatten all of them into a single sequence.&lt;/p&gt;

&lt;p&gt;At last, I can now subscribe the the sequence which will provide me a value which has (1) the ID of the call, (2) the success/failure of the call and (3) will raise a completion event only when all of the calls are done.&lt;/p&gt;

&lt;p&gt;Here is a sample trace of my test program. The server is hard coded to return a “400 Bad Request” on ID 7. Notice the client receives the responses out of order, handles the error condition, and notifies when everything is done.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/1/False"&gt;http://localhost:48963/api/favorites/1/False&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;False&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/2/True"&gt;http://localhost:48963/api/favorites/2/True&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;True&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/3/False"&gt;http://localhost:48963/api/favorites/3/False&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;False&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/4/True"&gt;http://localhost:48963/api/favorites/4/True&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;True&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/5/False"&gt;http://localhost:48963/api/favorites/5/False&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;False&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/6/True"&gt;http://localhost:48963/api/favorites/6/True&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;True&amp;quot;} 

  &lt;br /&gt; CLIENT: PUTing &lt;a href="http://localhost:48963/api/favorites/7/False"&gt;http://localhost:48963/api/favorites/7/False&lt;/a&gt; : {&amp;quot;value&amp;quot;:&amp;quot;False&amp;quot;} 

  &lt;br /&gt; SERVER: FavoritesController.Put(id:1, value:False) 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:7, value:False) 

  &lt;br /&gt;CLIENT: 1 succeeded = &amp;quot;False&amp;quot; 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:2, value:True) 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:5, value:False) 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:6, value:True) 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:3, value:False) 

  &lt;br /&gt;SERVER: FavoritesController.Put(id:4, value:True) 

  &lt;br /&gt;CLIENT: 2 succeeded = &amp;quot;True&amp;quot; 

  &lt;br /&gt;CLIENT: 7 failed 

  &lt;br /&gt;CLIENT: 6 succeeded = &amp;quot;True&amp;quot; 

  &lt;br /&gt;CLIENT: 5 succeeded = &amp;quot;False&amp;quot; 

  &lt;br /&gt;CLIENT: 4 succeeded = &amp;quot;True&amp;quot; 

  &lt;br /&gt;CLIENT: 3 succeeded = &amp;quot;False&amp;quot; 

  &lt;br /&gt;CLIENT: All completed&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10320742" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/PlkWsYKgGPQ" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/LINQ/">LINQ</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7Dev/">WP7Dev</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/reactive+extensions/">reactive extensions</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/rx/">rx</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2012/06/15/managing-multiple-web-service-calls-with-rx.aspx</feedburner:origLink></item><item><title>Give formatting its own line</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/94ZROZ-hf28/give-formatting-its-own-line.aspx</link><pubDate>Tue, 21 Feb 2012 14:51:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10270493</guid><dc:creator>BenWilli</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10270493</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10270493</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2012/02/21/give-formatting-its-own-line.aspx#comments</comments><description>&lt;p&gt;I got bitten by this not once but twice on Sunday as I hurriedly worked on a new phone app. Can you spot the difference between the the following two lines?&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a2620cbd-9319-41d5-abbe-f3474cfb6fe3" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 555px; height: 90px;background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;Uri uri &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Uri(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;/View/DataView.xaml?collection={0}&amp;amp;id={1}&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, collection, currentItem.getID(), UriKind.Relative));

Uri uri &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Uri(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;/View/DataView.xaml?collection={0}&amp;amp;id={1}&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, collection, currentItem.getID()), UriKind.Relative);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;br /&gt;

&lt;p&gt;Typically I like to put any String.Format() call on its own line because they tend to be long and sometimes complicated. This is an example of why. Notice the placement of the parenthesis? The first line compiles just fine, but it includes the UriKind as a parameter to String.Format and not to Uri(). Then at runtime the formatting just ignores it as “unused 3rd parameter” but when I try to navigate to this Uri in my phone app, the navigation service chokes because it requires a relative Uri. The error then says something like “can’t find view /View/DataView.xaml”. You look in that folder –yep the XAML is there. You look at the code – yep it seems to check out ok. This can turn into a real head scratcher.&lt;/p&gt;

&lt;p&gt;So anytime you call an API that has variable number of parameters like String.Format() its not a bad idea to do it on its own line like this&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fa6aafa7-1d9a-43ce-a886-16d452dc9f18" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 509px; height: 77px;background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; tmp &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;/View/DataView.xaml?collection={0}&amp;amp;id={1}&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, collection, currentItem.getID());

Uri uri &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Uri(tmp);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10270493" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/94ZROZ-hf28" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/Silverlight/">Silverlight</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/guidance/">guidance</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7Dev/">WP7Dev</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/tips/">tips</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2012/02/21/give-formatting-its-own-line.aspx</feedburner:origLink></item><item><title>Using Rx? Subscribe to exceptions!</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/t6lcnwQB7SY/using-rx-subscribe-to-exceptions.aspx</link><pubDate>Thu, 12 Jan 2012 22:05:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10256105</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10256105</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10256105</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2012/01/12/using-rx-subscribe-to-exceptions.aspx#comments</comments><description>&lt;p&gt;I have an app in the Windows Phone Marketplace which suddenly saw a spike in &lt;a href="http://blogs.msdn.com/b/andypennell/archive/2010/11/01/error-reporting-on-windows-phone-7.aspx" target="_blank"&gt;Little Watson&lt;/a&gt; email reports back to me last weekend. Each one looked like the last so I responded back to each user (always keep the user’s happy!) and went to investigate.&lt;/p&gt;  &lt;p&gt;The service it was talking to suddenly changed its behavior unexpectedly. This caused a ripple effect in that my WebClient code calls started to fail and one backwater section of my app did not have good error handling. So a service error (not my fault) caused the app to crash (my fault big time!). I had dealt with it nearly everywhere but missed it in one place in which it had never happened before.&lt;/p&gt;  &lt;p&gt;Using &lt;a href="http://blogs.msdn.com/b/benwilli/archive/2011/11/01/populating-windows-phone-view-models-with-rx.aspx" target="_blank"&gt;the Rx pattern discussed before&lt;/a&gt; my code looked something like this&lt;/p&gt;  &lt;div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:9abcf7c3-40f6-4eb9-95f2-79445b221dec" class="wlWriterSmartContent"&gt;   &lt;pre style="width: 396px; height: 60px; overflow: auto; background-color: white;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;var outstandingRequest &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; svc.GetDataFromWeb(id)
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;    .SubscribeOnDispatcher()
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;    .Subscribe( data &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DoSomething(data));&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Notice something? I’m not using the overload of Subscribe() which provides the exception handling. If you are subscribing, its a good idea to grab the errors that come out of the observable. Don’t do it and the phone app crashes.&lt;/p&gt;

&lt;p&gt;A small but important change handles the exception and solved the problem. (In reality I added a little more complex logic than shown here so that it would retry the request to the service). Next step was to &lt;em&gt;&lt;strong&gt;quickly&lt;/strong&gt; &lt;/em&gt;get it into the marketplace. &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:64309125-32c8-46e2-817d-5b9f48bb9b1e" class="wlWriterSmartContent"&gt;
  &lt;pre style="width: 518px; height: 135px; overflow: auto; background-color: white;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;1&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;var outstandingRequest &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; svc.GetDataFromWeb(id)
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;2&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;    .SubscribeOnDispatcher()
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;3&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;    .Subscribe( 
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;4&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;        data &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DoSomething(data),
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;5&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;        ex &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { App.LogError(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Error loading data {0}: {1}&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, id, ex),
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;6&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;        () &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 128);"&gt;7&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;    );&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately the marketplace crash data isn't real time so I didn’t have this graph available until yesterday. That hockey stick shape is not something you want to see on your dashboard!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/3438.image_5F00_423BD28B.png"&gt;&lt;img style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-78-metablogapi/0218.image_5F00_thumb_5F00_4F35B29C.png" width="462" height="277" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully the users weren't scared away from the app by the experience. Its easier to get them to come in the first place than it is to get them to come back after a bad experience.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10256105" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/t6lcnwQB7SY" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7Dev/">WP7Dev</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/reactive+extensions/">reactive extensions</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/rx/">rx</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2012/01/12/using-rx-subscribe-to-exceptions.aspx</feedburner:origLink></item><item><title>Rx ensures Unsubscribe</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/8U5J-I4JQtM/rx-ensures-unsubscribe.aspx</link><pubDate>Tue, 08 Nov 2011 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10234877</guid><dc:creator>BenWilli</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10234877</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10234877</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2011/11/08/rx-ensures-unsubscribe.aspx#comments</comments><description>&lt;p&gt;I recently had an ObservableCollection which did not currently contain the item I was interested in, but it would sometime in the near future. The questions is how best to get notified that the item is available so I could access its full property set?&amp;#160; I only had the item’s ID and the collection’s CollectionChanged event to work with. &lt;/p&gt;  &lt;p&gt;My first instinct was to just go with simple lambda expression subscribed to the CollectionChanged event in order to filter for the item’s appearance in the collection&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:41d76add-cab9-4192-9419-a9098b1d7b00" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; WaitForMovie_UsingLambda(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; ID, &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; collection)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    collection.CollectionChanged += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(&lt;/li&gt; &lt;li&gt;        (sender, e) =&amp;gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (e.Action == &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Add)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; movie = ((&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;)e.NewItems[0]);&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (movie.ID == movieSearchID)&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li&gt;                    DoSomething(movie);&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li&gt;        });&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;   &lt;br /&gt;The problem with this pattern is that you can’t unsubscribe from the event notification. Every time something is added (even after we have found our item) this logic will execute wasting CPU processing power. In addition, this also has the potential to create memory issues/leaks because objects may remain alive longer since the subscription will exist as long as the collection does. If the subscription has side effects (there are none in this simple case) then they are essentially permanent.&lt;/p&gt;  &lt;p&gt;We can go with a basic delegate structure which allows us to unsubscribe but the logic gets a little more complicated and we have to spread it out into multiple sections.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:03421b31-526d-4114-9ac5-7075c1418b90" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 500px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; movieSearchID;&lt;/li&gt; &lt;li class="even"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; movieSearchCollection;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li class="even"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; WaitForMovie_UsingDelegate(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; ID, &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; collection)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li class="even"&gt;    movieSearchID = ID;&lt;/li&gt; &lt;li&gt;    collection.CollectionChanged += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(moveCollectionChanged);&lt;/li&gt; &lt;li class="even"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li class="even"&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; moveCollectionChanged(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (e.Action == &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Add)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li class="even"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; movie = ((&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;)e.NewItems[0]);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (movie.ID == movieSearchID)&lt;/li&gt; &lt;li class="even"&gt;        {&lt;/li&gt; &lt;li&gt;            DoSomething(movie);&lt;/li&gt; &lt;li class="even"&gt;            movieSearchCollection.CollectionChanged -= &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(moveCollectionChanged);&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li class="even"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;   &lt;br /&gt;But now we’ve lost the encapsulation the lambda provided us so its harder to read. We also have to be careful because the code isn’t reentrant due to the state variables. &lt;/p&gt;  &lt;p&gt;Now lets look at the Reactive Extensions solution&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:12ebfe7a-52ff-4144-9ef6-e80a98702afa" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; WaitForMovie_UsingRx(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; ID, &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; collection)&lt;/li&gt; &lt;li class="even"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; changes = &lt;span style="color:#2b91af"&gt;Observable&lt;/span&gt;.FromEventPattern&amp;lt;&lt;span style="color:#2b91af"&gt;NotifyCollectionChangedEventArgs&lt;/span&gt;&amp;gt;(collection, &lt;span style="color:#a31515"&gt;&amp;quot;CollectionChanged&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; itemWeWant = &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; change &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; changes&lt;/li&gt; &lt;li&gt;                     &lt;span style="color:#0000ff"&gt;where&lt;/span&gt; change.EventArgs.Action == &lt;span style="color:#2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Add&lt;/li&gt; &lt;li class="even"&gt;                     &lt;span style="color:#0000ff"&gt;let&lt;/span&gt; movie = ((&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;)change.EventArgs.NewItems[0])&lt;/li&gt; &lt;li&gt;                     &lt;span style="color:#0000ff"&gt;where&lt;/span&gt; movie.ID == ID&lt;/li&gt; &lt;li class="even"&gt;                     &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; movie;&lt;/li&gt; &lt;li&gt;    itemWeWant.Take(1).Subscribe( movie =&amp;gt; DoSomething(movie));&lt;/li&gt; &lt;li class="even"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/p&gt;  &lt;p&gt;The Take(1) ensures we only have a single result. This causes the observable to complete after that one result. When observables complete, the subscription to the event is unsubscribed. Plus we don’t have any state to manage ourselves and the code is short and concise. It is readable and directly expresses our intended purpose to the reader.&lt;/p&gt;  &lt;p&gt;This is an excellent pattern for any type of .NET event where you expect to receive a specific number of notifications instead of a permanent stream of notifications and you don’t want to worry about managing state or cleaning up your delegates.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Implementation detail notes&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;In this case I know that only one item is being added to the collection at a time. This allows me to just look at NewItems[0] instead of the whole NewItems collection. If that wasn’t the case we could add logic to filter the entire NewItems collection as well, but it wouldn’t change the overall point.&lt;/li&gt;    &lt;li&gt;If you have read some of my other blog posts on Rx you might notice that sometimes I use FromEventPattern and sometimes I use FromEvent. They are essentially the same. The reason it varies depends on what type of project I am working on. FromEvent is used in the phone’s Rx library while the desktop Rx library has evolved since the phone was released and has changed the name of the method to FromEventPattern.     &lt;br /&gt;&lt;/li&gt; &lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10234877" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/8U5J-I4JQtM" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/LINQ/">LINQ</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/reactive+extensions/">reactive extensions</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/rx/">rx</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2011/11/08/rx-ensures-unsubscribe.aspx</feedburner:origLink></item><item><title>Windows Phone travel tip – pin your map locations</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/wvaSHj-zN6A/windows-phone-travel-tip-pin-your-map-locations.aspx</link><pubDate>Mon, 07 Nov 2011 19:02:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10234706</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10234706</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10234706</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2011/11/07/windows-phone-travel-tip-pin-your-map-locations.aspx#comments</comments><description>&lt;p&gt;Whenever I’m travelling I always have two freshly pinned tiles at the bottom of my start screen. Before I leave, I use my phone to look up the hotel I’m staying at and the location I’m visiting (like a customer’s address) and pin them both to the start screen. That way while I’m in an unfamiliar city I can easily find my way back to either of the two major way points during my trip without needing to dig through old emails to find the addresses. Just click the hotel tile, then click directions and I instantly know how to find my way back to a comfortable bed. &lt;/p&gt;  &lt;p&gt;Bonus points if you also add an additional tile for the local weather!&lt;/p&gt;  &lt;p&gt;(posted from 35,000 feet with my phone already ready to guide me through the city when I land)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10234706" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/wvaSHj-zN6A" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/Travel/">Travel</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2011/11/07/windows-phone-travel-tip-pin-your-map-locations.aspx</feedburner:origLink></item><item><title>Populating Windows Phone view models with Rx</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/_WtzSutZaws/populating-windows-phone-view-models-with-rx.aspx</link><pubDate>Tue, 01 Nov 2011 23:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10232273</guid><dc:creator>BenWilli</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10232273</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10232273</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2011/11/01/populating-windows-phone-view-models-with-rx.aspx#comments</comments><description>&lt;p&gt;I’ve gotten rather fond of using the &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609"&gt;Reactive Extensions&lt;/a&gt; for populating my data from the web in my Windows Phone applications. They provide a lot of flexibility and the code stays in a readable form.&lt;/p&gt;  &lt;p&gt;To demonstrate in a traditional MVVM model, let’s drop some movie information from Netflix onto our Windows Phone screen. First we need a structure to hold that movie data.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74bed967-d9db-481a-9d65-b6ca0d76f8e7" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;struct&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Description { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;We also need a view model for the view to bind to. The view model will expose two interesting properties for the the UI use. The NetworkBusy property can be bound to a progress bar to automatically show/hide the bar while the underlying network service is busy. The Movies property can be bound to a listbox so that the user can scroll through the list.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e3a13886-ce88-450b-9727-82016fa32b27" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; Movies { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li class="even"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; _networkBusy = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li class="even"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; NetworkBusy { &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; _networkBusy; }&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (_networkBusy != &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;)&lt;/li&gt; &lt;li class="even"&gt;        {&lt;/li&gt; &lt;li&gt;            _networkBusy = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li class="even"&gt;            RaisePropertyChangedNotification(&lt;span style="color:#a31515"&gt;&amp;quot;NetworkBusy&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li class="even"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If our service class provides the view model with an IObservable&amp;lt;MovieData&amp;gt; when we ask for all the movies, we can just subscribe to the stream of MovieData objects it provides and add them to our Movies collection. Furthermore, since an Observable sequence provides error and completion notifications, we will know exactly when to disable the progress bar the UI uses by setting NetworkBusy to false. We can also take action on errors as needed. So we’d like it to be as simple as this.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c2e45209-ea1a-434e-b09c-b3644c9794bb" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; LoadData()&lt;/li&gt; &lt;li class="even"&gt;{&lt;/li&gt; &lt;li&gt;    NetworkBusy = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li class="even"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    Movies.Clear();&lt;/li&gt; &lt;li class="even"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; svc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MovieService&lt;/span&gt;();&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; results = svc.GetMovies();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li class="even"&gt;    results.ObserveOnDispatcher().Subscribe(&lt;/li&gt; &lt;li&gt;                item =&amp;gt; Movies.Add(item),&lt;/li&gt; &lt;li class="even"&gt;                ex =&amp;gt; { LogException(ex); NetworkBusy = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;; },&lt;/li&gt; &lt;li&gt;                () =&amp;gt; NetworkBusy = &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;            );&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Also, note that the only thing we need to do to avoid accessing the UI on the wrong thread is to add ObserveOnDispatcher() to the mix.&lt;/p&gt;  &lt;p&gt;Next we need to build the method on the MovieService class that will generate the Observable stream of MovieData objects for the view model. In this case we will return all the films that have a rating greater than 4. Here is the code for the method. I’ll break it down after the snippet.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8ea3c245-7ea6-43ca-88a1-54fe9c78c5d1" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background: #ddd; max-height: 500px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;IObservable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&amp;gt; GetData()&lt;/li&gt; &lt;li class="even"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// create the observable for the download event&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; wc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; o = &lt;span style="color:#2b91af"&gt;Observable&lt;/span&gt;.FromEvent&amp;lt;&lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventArgs&lt;/span&gt;&amp;gt;(wc, &lt;span style="color:#a31515"&gt;&amp;quot;DownloadStringCompleted&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li class="even"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// initiate the transfer&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;    wc.DownloadStringAsync(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;http://odata.netflix.com/Catalog/Titles?$filter=AverageRating gt 4&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li class="even"&gt;    &lt;span style="color:#008000"&gt;// convert string to data we are interested in&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; results = o.Take(1).SelectMany(item =&amp;gt;&lt;/li&gt; &lt;li class="even"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;// use LINQ to XML to project the XML nodes&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;        &lt;span style="color:#008000"&gt;// to a collection of MovieData objects&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; doc = &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt;.Parse(item.EventArgs.Result);&lt;/li&gt; &lt;li class="even"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; movies = &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; movie &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; doc.Descendants(&lt;span style="color:#2b91af"&gt;XName&lt;/span&gt;.Get(&lt;span style="color:#a31515"&gt;&amp;quot;entry&amp;quot;&lt;/span&gt;,&lt;span style="color:#a31515"&gt;&amp;quot;http://www.w3.org/2005/Atom&amp;quot;&lt;/span&gt;))&lt;/li&gt; &lt;li&gt;                     &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MovieData&lt;/span&gt;&lt;/li&gt; &lt;li class="even"&gt;                     {&lt;/li&gt; &lt;li&gt;                         Name = movie.Element(&lt;span style="color:#2b91af"&gt;XName&lt;/span&gt;.Get(&lt;span style="color:#a31515"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;http://www.w3.org/2005/Atom&amp;quot;&lt;/span&gt;)).Value,&lt;/li&gt; &lt;li class="even"&gt;                         Description = movie.Element(&lt;span style="color:#2b91af"&gt;XName&lt;/span&gt;.Get(&lt;span style="color:#a31515"&gt;&amp;quot;summary&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;http://www.w3.org/2005/Atom&amp;quot;&lt;/span&gt;)).Value&lt;/li&gt; &lt;li&gt;                     };&lt;/li&gt; &lt;li class="even"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; movies;&lt;/li&gt; &lt;li&gt;    });&lt;/li&gt; &lt;li class="even"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; results;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Lines 4-5: create the WebClient and subscribe to the event in a manner that provides us an Observable. Because this wraps the even, ‘o’ is of type IObservable&amp;lt;DownloadStringCompletedEventArgs&amp;gt;&lt;/p&gt;  &lt;p&gt;Once the download is initiated we provide some filtering and projection to the current sequence to transform it to an IObservable&amp;lt;MovieData&amp;gt; and return that to the user. The consumer doesn’t want to deal with EventArgs, he wants MovieData!&lt;/p&gt;  &lt;p&gt;Line 11: &lt;strong&gt;Take(1)&lt;/strong&gt; - Normally observables wrapped around events never complete. Adding Take(1) provides us the completion event because we know the event will only be raised once.&amp;#160; 1 web request = 1 web response.&lt;/p&gt;  &lt;p&gt;Line 11: &lt;strong&gt;SelectMany(…) &lt;/strong&gt;– The single response of the web request is a string that contains multiple movies. If we just did Select() and returned a collection of MovieData objects, the consumer would have to loop over collection. If we use SelectMany(), the collection gets flattened out and we will return just the MovieData objects in the sequence.&lt;/p&gt;  &lt;p&gt;Lines 15-21: take the resulting downloaded string and transform it with LINQ to XML into a collection of MovieData objects&lt;/p&gt;  &lt;p&gt;Additional Fun: This on its face is a nice enough pattern, but there are a couple of others patterns which can emerge nicely on top of this one.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If you create multiple requests at the same time you can easily compose an observable from all of them in order to determine when all of them are complete. &lt;/li&gt;    &lt;li&gt;With a single stream, you can create multiple sub streams to populate things like “first 10 items” on your front page or create filtered lists for pivots very easily. &lt;/li&gt;    &lt;li&gt;If you leave out the Take(1) you leave open the possibility to reuse the WebClient again and continue to push additional pages of data back into the view model without the view model needing to be aware of the paging going on – its just waiting for a stream of MovieData’s &lt;/li&gt; &lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10232273" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/_WtzSutZaws" height="1" width="1"/&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-23-22-73/PhoneApp1.zip" length="27157" type="application/zip" /><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/LINQ/">LINQ</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7Dev/">WP7Dev</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/design/">design</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/reactive+extensions/">reactive extensions</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2011/11/01/populating-windows-phone-view-models-with-rx.aspx</feedburner:origLink></item><item><title>Type, don’t look!</title><link>http://feedproxy.google.com/~r/msdn/BrainDump/~3/T6EYrU3pFBg/type-don-t-look.aspx</link><pubDate>Thu, 18 Aug 2011 19:00:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10197474</guid><dc:creator>BenWilli</dc:creator><slash:comments>3</slash:comments><wfw:commentRss>http://blogs.msdn.com/b/benwilli/rsscomments.aspx?WeblogPostID=10197474</wfw:commentRss><wfw:comment>http://blogs.msdn.com/b/benwilli/commentapi.aspx?WeblogPostID=10197474</wfw:comment><comments>http://blogs.msdn.com/b/benwilli/archive/2011/08/18/type-don-t-look.aspx#comments</comments><description>&lt;p&gt;Just a quick tip for Windows Phone users regarding the onscreen keyboard. Most people I’ve talked to who complain about using an onscreen keyboard have a common issue. &lt;strong&gt;They are looking at what they type. &lt;/strong&gt;This is their biggest mistake. When I first started using my Samsung Focus device I was getting a little frustrated with the results of my typing as well. I’d hit a wrong key then back up and fix it then hit another wrong key and back up and fix it too. Then I discovered the secret. Don’t look at what you are typing until you are done. This provided me two benefits: (1) I can look at the keyboard instead which helps while you are getting used to the size of a different keyboard and (2) I started to let the phone’s amazing keyboard do its thing.&lt;/p&gt;  &lt;p&gt;Stop fighting it. Its &lt;a href="http://research.microsoft.com/en-us/news/features/wp7keyboard-042811.aspx"&gt;really very smart.&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Don’t try to out smart it. Blaze away at the keys as best as you can and then look up only when your sentence is finished. I think you’ll be pleasantly surprised at how few corrections you have to make at this point… and you just might start to impress people with your newly found speed texting skills.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10197474" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/msdn/BrainDump/~4/T6EYrU3pFBg" height="1" width="1"/&gt;</description><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/windows+phone+7/">windows phone 7</category><category domain="http://blogs.msdn.com/b/benwilli/archive/tags/WP7/">WP7</category><feedburner:origLink>http://blogs.msdn.com/b/benwilli/archive/2011/08/18/type-don-t-look.aspx</feedburner:origLink></item></channel></rss>
