<?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>Andy McMullan</title>
	<atom:link href="http://www.andymcm.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.andymcm.com</link>
	<description></description>
	<lastBuildDate>Fri, 30 Apr 2010 22:21:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Counting objects with Unity</title>
		<link>http://www.andymcm.com/?p=56</link>
		<comments>http://www.andymcm.com/?p=56#comments</comments>
		<pubDate>Fri, 30 Apr 2010 22:17:22 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[IOC]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/?p=56</guid>
		<description><![CDATA[I’m using Unity for the first time on my current project (a WCF service), and the project is now fairly large with a significant amount of container configuration.&#160; I’m taking advantage of lifetime managers to optimise object reuse, with PerThreadLifetimeManager being particularly attractive in a service – less garbage through object reuse but no locking [...]]]></description>
			<content:encoded><![CDATA[<p>I’m using <a href="http://unity.codeplex.com/">Unity</a> for the first time on my current project (a WCF service), and the project is now fairly large with a significant amount of container configuration.&nbsp; I’m taking advantage of lifetime managers to optimise object reuse, with PerThreadLifetimeManager being particularly attractive in a service – less garbage through object reuse but no locking to worry about.</p>
<p>I decided that it might be nice to see counts of the number of objects that Unity is creating for each type, to confirm that my lifetime configuration is optimal.&nbsp; It turned out to be straightforward to do this with a Unity extension.</p>
<p>Here’s how the extension is used.&nbsp; First, the extension is added to the container:</p>
<pre class="csharpcode">    var objectCounterExtension = <span class="kwrd">new</span> ObjectCounterExtension();
    container.AddExtension(objectCounterExtension);
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>&nbsp;</p>
<p>At application shutdown a list of types and their associated instance counts can be output like this:</p>
<pre class="csharpcode">    IDictionary&lt;Type,<span class="kwrd">int</span>&gt; objectCounts = objectCounterExtension.ObjectCounts;

    <span class="kwrd">foreach</span> (var objectCount <span class="kwrd">in</span> objectCounts.OrderBy(oc =&gt; oc.Value))
    {
        Type type = objectCount.Key;
        <span class="kwrd">int</span> count = objectCount.Value;
        Console.WriteLine(<span class="str">"{0}: {1}"</span>, type, count);
    }</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>&nbsp;</p>
<p>The extension is quite simple, once you know how:</p>
<pre class="csharpcode">    <span class="kwrd">class</span> ObjectCounterExtension : UnityContainerExtension
    {
        ObjectCounterStrategy objectCounterStrategy;

        <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()
        {
            objectCounterStrategy = <span class="kwrd">new</span> ObjectCounterStrategy();
            Context.Strategies.Add(objectCounterStrategy, UnityBuildStage.Creation);
        }

        <span class="kwrd">public</span> IDictionary&lt;Type, <span class="kwrd">int</span>&gt; ObjectCounts
        {
            get { <span class="kwrd">return</span> objectCounterStrategy.ObjectCounts; }
        }
    }

    <span class="kwrd">class</span> ObjectCounterStrategy : IBuilderStrategy
    {
        Dictionary&lt;Type, <span class="kwrd">int</span>&gt; objectCounts = <span class="kwrd">new</span> Dictionary&lt;Type,<span class="kwrd">int</span>&gt;();

        <span class="kwrd">public</span> <span class="kwrd">void</span> PostBuildUp(IBuilderContext context)
        {
            IBuildKey buildKey = context.BuildKey <span class="kwrd">as</span> IBuildKey;
            <span class="kwrd">if</span> (buildKey == <span class="kwrd">null</span>)
                <span class="kwrd">return</span>;

            Type type = buildKey.Type;

            <span class="kwrd">lock</span> (objectCounts)
            {
                <span class="kwrd">int</span> count;
                <span class="kwrd">if</span> (objectCounts.TryGetValue(type, <span class="kwrd">out</span> count))
                {
                    objectCounts[type] = count + 1;
                }
                <span class="kwrd">else</span>
                {
                    objectCounts[type] = 1;
                }
            }
        }

        <span class="kwrd">public</span> Dictionary&lt;Type, <span class="kwrd">int</span>&gt; ObjectCounts
        {
            get
            {
                <span class="kwrd">lock</span> (objectCounts)
                {
                    <span class="kwrd">return</span> <span class="kwrd">new</span> Dictionary&lt;Type, <span class="kwrd">int</span>&gt;(objectCounts);
                }
            }
        }

        <span class="kwrd">public</span> <span class="kwrd">void</span> PostTearDown(IBuilderContext context) {}
        <span class="kwrd">public</span> <span class="kwrd">void</span> PreBuildUp(IBuilderContext context) {}
        <span class="kwrd">public</span> <span class="kwrd">void</span> PreTearDown(IBuilderContext context) {}
    }</pre>
<pre class="csharpcode">&nbsp;</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=56</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>32-bit client can’t connect to 64-bit COM server</title>
		<link>http://www.andymcm.com/?p=46</link>
		<comments>http://www.andymcm.com/?p=46#comments</comments>
		<pubDate>Wed, 10 Mar 2010 23:48:36 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[DCOM]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/?p=46</guid>
		<description><![CDATA[In a previous post I described how to host a COM server in a managed process using RegistrationServices.RegisterTypeForComClients.&#160; I’ve been using this approach successfully for a while, but today I hit a snag.&#160;&#160; I changed my C# server process from 32-bit to 64-bit, and immediately my 32-bit C++ client could no longer connect. In theory [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.andymcm.com/?p=15">previous post</a> I described how to host a COM server in a managed process using RegistrationServices.RegisterTypeForComClients.&#160; I’ve been using this approach successfully for a while, but today I hit a snag.&#160;&#160; I changed my C# server process from 32-bit to 64-bit, and immediately my 32-bit C++ client could no longer connect.</p>
<p>In theory it shouldn’t matter to the client whether the server is 32-bit or 64-bit – everything is out-of-process so there is no compatibility issue.&#160;&#160; But I could see that COM was refusing to allow my client to connect to the running 64-bit server process, and instead was trying to launch a new server process (which was failing because I don’t allow that).</p>
<p>I have seen this type of problem many times before with COM, and it’s almost always due to security configuration – specifically the ‘run as’ configuration of the server.&#160;&#160; So I spent a lot of time investigating that, but it turned out to be something much simpler.&#160; Since Windows 2003 SP1, COM has a rule on x64 that if a 32-bit client CoCreates an out-of-proc server, COM will try to connect to a 32-bit server.&#160; If the client is 64-bit, COM will try to connect to a 64-bit server.&#160; So in my case, COM could see that the 64-bit server was running, but because the client was 32-bit it decided to launch a new (hopefully 32-bit) server process to service the request.</p>
<p>Fortunately there are two easy ways around the problem.&#160; The first option is to modify the client to specify&#160; <b>CLSCTX_ACTIVATE_64_BIT_SERVER</b> in the CoCreateInstance call.&#160; The other (probably better) option is to add a <b>PreferredServerBitness</b> flag to the AppID registry entry for the server.</p>
<p>CLSCTX_ACTIVATE_64_BIT_SERVER is described <a href="http://msdn.microsoft.com/en-us/library/ms693716%28v=VS.85%29.aspx">here</a>, and PreferredServerBitness <a href="http://msdn.microsoft.com/en-us/library/ms694319%28VS.85%29.aspx">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=46</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debug mode on the Pentax K20D DSLR</title>
		<link>http://www.andymcm.com/?p=22</link>
		<comments>http://www.andymcm.com/?p=22#comments</comments>
		<pubDate>Tue, 05 Jan 2010 22:08:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=22</guid>
		<description><![CDATA[I really like my Pentax K20D DSLR, but when I first got it the autofocus was not accurate, particularly with my favourite FA 50mm 1.4 lens.&#160;&#160; Fortunately the camera has an autofocus adjustment in the standard menu, which fixed the problem, but I recently discovered that there is a hidden ‘debug’ menu that offers even [...]]]></description>
			<content:encoded><![CDATA[<p>I really like my Pentax K20D DSLR, but when I first got it the autofocus was not accurate, particularly with my favourite FA 50mm 1.4 lens.&nbsp;&nbsp; Fortunately the camera has an autofocus adjustment in the standard menu, which fixed the problem, but I recently discovered that there is a hidden ‘debug’ menu that offers even more control over autofocus.&nbsp;&nbsp; However the procedure to turn on the debug menu is a little fiddly, so I thought I’d document it here.</p>
<p>[Note that these instructions <strong>only apply to the K20D</strong>.&nbsp;&nbsp; Other Pentax and Samsung cameras also have a debug mode, but the instructions are slightly different for each camera – see the end of the post for links.]</p>
<p><strong>Step 1</strong>: Create a text file called MODSET.442 in the root of your SD card.&nbsp;&nbsp; The file should contain the following single line:</p>
<p>[OPEN_DEBUG_MENU]</p>
<p>You can use any plain text editor, for example Windows Notepad.&nbsp; Make sure that the file is called MODSET.442 and <strong>not</strong> MODSET.442.txt</p>
<p><strong>Step 2</strong>: Put the SD card into the camera but <strong>leave the card door open</strong>.</p>
<p><strong>Step 3</strong>: Hold down the Menu button and turn on the camera.&nbsp; Keep holding down the Menu button until the debug menu appears.</p>
<p><a href="http://lh5.ggpht.com/_9JcejghnbMY/S0O4RQLYZ_I/AAAAAAAAGAU/FmWE9Ejp_QA/s1600-h/debugmode1%5B3%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="debugmode1" border="0" alt="debugmode1" src="http://lh6.ggpht.com/_9JcejghnbMY/S0O4R2iTayI/AAAAAAAAGAY/FtgVPaB2IeA/debugmode1_thumb%5B1%5D.jpg?imgmax=800" width="404" height="290"></a> </p>
<p><strong>Step 4:&nbsp; </strong>Close the SD card door.</p>
<p><strong>Step 5</strong>: To enable debug mode, press the right arrow to change Debug Mode from DIS to EN, then press the OK button.</p>
<p><a href="http://lh4.ggpht.com/_9JcejghnbMY/S0O4SkDWw0I/AAAAAAAAGAc/ybADXg7jyZs/s1600-h/debugmode2%5B3%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="debugmode2" border="0" alt="debugmode2" src="http://lh4.ggpht.com/_9JcejghnbMY/S0O4THEQINI/AAAAAAAAGAg/MSNbwHQsi3s/debugmode2_thumb%5B1%5D.jpg?imgmax=800" width="404" height="292"></a> </p>
<p><strong>Step 6</strong>: Press the MENU button to get to the standard camera menu.&nbsp;&nbsp; (If nothing happens, make sure you have closed the SD card door.&nbsp;&nbsp; If necessary, turn the camera off and on again after closing the door.)</p>
<p>Press the right arrow twice to get to the Setup menu:</p>
<p><a href="http://lh4.ggpht.com/_9JcejghnbMY/S0O4T0OlLGI/AAAAAAAAGAk/CO-JRChYwpE/s1600-h/debugmode3%5B3%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="debugmode3" border="0" alt="debugmode3" src="http://lh5.ggpht.com/_9JcejghnbMY/S0O4UcTE4YI/AAAAAAAAGAo/YK8oB3vhd-8/debugmode3_thumb%5B1%5D.jpg?imgmax=800" width="404" height="280"></a> </p>
<p>This is just the standard Setup menu, but some new items have been added to the bottom.&nbsp; Press the up arrow to quickly jump to the bottom to see them.</p>
<p><a href="http://lh6.ggpht.com/_9JcejghnbMY/S0O4UyrntUI/AAAAAAAAGAs/LyI-ozkbb2U/s1600-h/debugmode4%5B3%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="debugmode4" border="0" alt="debugmode4" src="http://lh6.ggpht.com/_9JcejghnbMY/S0O4VU5Nw2I/AAAAAAAAGAw/_P6IsYxZuzE/debugmode4_thumb%5B1%5D.jpg?imgmax=800" width="404" height="275"></a> </p>
<p>Select AF TEST then click the right arrow button.</p>
<p><a href="http://lh5.ggpht.com/_9JcejghnbMY/S0O4V2Je4QI/AAAAAAAAGA0/Qq5X9goR46o/s1600-h/debugmode5%5B3%5D.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="debugmode5" border="0" alt="debugmode5" src="http://lh4.ggpht.com/_9JcejghnbMY/S0O4Wbi7rKI/AAAAAAAAGA4/v99L5J6LavU/debugmode5_thumb%5B1%5D.jpg?imgmax=800" width="404" height="292"></a> </p>
<p>You can now set the global focus correction.&nbsp; In this screenshot the correction is set to –90 and I am about to increase it by 20 (making it –70).&nbsp;&nbsp; Press the OK button and you are ready to take a test shot with your new AF setting.&nbsp; (Some reports suggest that the camera must be restarted after setting the correction value, but it seems to apply immediately on my camera.)</p>
<p><strong>Step 7</strong>:&nbsp; Once you have finished playing with the AF correction, you will want to turn off the debug menu.&nbsp; To do so, just turn the camera on and when the debug menu appears, set DEBUG MODE to DIS and click OK.&nbsp;&nbsp; Your camera should now be totally back to normal. </p>
<p>You can leave the MODSET.442 on your SD card and then re-enable the debug menu at any time by starting from step 2.&nbsp; Or you can just delete the file if you prefer.</p>
<p>If some part of the instructions doesn’t seem to be working, check your SD card door.&nbsp;&nbsp; For some steps it must be open, and others it must be closed – follow the instructions exactly as above.</p>
<p>The debug menu is also available on several other Pentax and Samsung cameras – see the <a href="http://www.pentax-hack.info/documents/debug.html">pentax-hack</a> site for details.</p>
<p>Also there are detailed instructions for the Pentax K-x <a href="http://deejjjaaaa.blogspot.com/2009/12/pentax-kx-debug-menu-to-do-focus.html">here</a>, which I found very useful, even though the steps are slightly different than the K20D.</p>
<p>After many many test shots I think I’ve settled on –90 as my correction value.&nbsp;&nbsp; That allows me to run my FA 50mm 1.4 with no ‘standard’ correction, and my other lenses seem happy too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=22</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using ANTLR to parse boolean queries</title>
		<link>http://www.andymcm.com/?p=21</link>
		<comments>http://www.andymcm.com/?p=21#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:26:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=21</guid>
		<description><![CDATA[ANTLR is a well-known parser generator.  You supply it with a grammar and it builds a lexer and parser in the programming language of your choice.   I’ve just spent a few hours getting to grips with ANTLR basics, so I thought I’d document it for future reference. My basic requirement is to transform an end-user [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.antlr.org">ANTLR</a> is a well-known parser generator.  You supply it with a grammar and it builds a lexer and parser in the programming language of your choice.   I’ve just spent a few hours getting to grips with ANTLR basics, so I thought I’d document it for future reference.</p>
<p>My basic requirement is to transform an end-user boolean query to an XML document.   A simple example of a query I need to parse is:</p>
<p><span style="font-family: Courier New;"> john AND (joe OR sue)</span></p>
<p>I want the generated parser to produce the following syntax tree for this query:</p>
<pre>
AND
      john
      OR
            joe
            sue
</pre>
<p>It should then be trivial to walk the tree and write the XML document I need.</p>
<p>An ANTLR grammar is specified in a text file with a .g extension.   Here’s how my SimpleBoolean.g starts:</p>
<pre class="csharpcode">grammar SimpleBoolean;

options
{
  language = CSharp2;
  output = AST;
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->I’m specifying that the generated code should be C#, and that the parser should build an abstract syntax tree (AST).</p>
<p>Next I specify the lexer rules:</p>
<pre class="csharpcode">LPAREN : <span class="str">'('</span> ;
RPAREN : <span class="str">')'</span> ;
AND : <span class="str">'AND'</span>;
OR : <span class="str">'OR'</span>;
WS :  ( <span class="str">' '</span> | <span class="str">'\t'</span> | <span class="str">'\r'</span> | <span class="str">'\n'</span>) {$channel=HIDDEN;}  ;
WORD :  (~( <span class="str">' '</span> | <span class="str">'\t'</span> | <span class="str">'\r'</span> | <span class="str">'\n'</span> | <span class="str">'('</span> | <span class="str">')'</span> ))*;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>These rules are used to turn the source text into a stream of tokens.  So parentheses and operators are special tokens, and anything else is either whitespace (which is skipped) or a word.</p>
<p>Finally I specify the parser rules:</p>
<pre class="csharpcode">expr : andexpr;
andexpr : orexpr (AND^ orexpr)*;
orexpr : atom (OR^ atom)*;
atom : WORD | LPAREN! expr RPAREN!;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p>This is the confusing bit.  The chain of expressions (expr –&gt; andexpr –&gt; orexpr) is used to signify precedence.  In this case I’ve made OR higher precedence than AND.   Also notice that andexpr (for example) does not <strong>require</strong> an AND – it is optional.  This is why the grammar supports an expr containing only an OR, even though expr is defined in terms of andexpr.</p>
<p>The symbol suffixes (^ on the operators and ! on the parentheses) are there to direct the AST generation.  ^ signifies a tree branch, whereas ! indicates something that should be omitted from the tree.   Anything else is a leaf.   (The parentheses are omitted because they are redundant – the structure of the tree gives the order of evaluation implicitly.)</p>
<p>So SimpleBoolean.g is now complete, and all that remains is to ask ANTLR to generate a C# parser from it.  I used the <a href="http://www.antlr.org/works/">ANTLRWorks</a> IDE for this, but you could use the command-line.   Once the C# files are generated, and the <a href="http://www.antlr.org/download/CSharp">ANTLR .NET runtimes files</a> added to a C# project, we’re ready to write some C#.   Here’s some code that parses a query and then walks the syntax tree and writes the nodes to the console:</p>
<pre class="csharpcode"><span class="kwrd">class</span> Program
{
    <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
    {
        ANTLRStringStream expression = <span class="kwrd">new</span> ANTLRStringStream(<span class="str">"john AND (joe OR sue)"</span>);
        var tokens = <span class="kwrd">new</span> CommonTokenStream(<span class="kwrd">new</span> SimpleBooleanLexer(expression));
        var parser = <span class="kwrd">new</span> SimpleBooleanParser(tokens);

        SimpleBooleanParser.expr_return ret = parser.expr();
        CommonTree ast = (CommonTree)ret.Tree;
        Print(ast,0);
    }

    <span class="kwrd">static</span> <span class="kwrd">void</span> Print(CommonTree tree, <span class="kwrd">int</span> level)
    {
        Console.WriteLine(<span class="kwrd">new</span> <span class="kwrd">string</span>(<span class="str">'\t'</span>, level) + tree.Text);
        <span class="kwrd">if</span> (tree.Children != <span class="kwrd">null</span>)
            <span class="kwrd">foreach</span> (CommonTree child <span class="kwrd">in</span> tree.Children)
                Print(child,level+1);
    }
}</pre>
<p>which prints the following:</p>
<pre class="csharpcode">AND
        john
        OR
                joe
                sue</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->For a more comprehensive ANTLR tutorial, see <a href="http://www.alittlemadness.com/2006/06/05/antlr-by-example-part-1-the-language/">this series of blog posts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=21</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity’s VirtualMethodInterceptor with internal classes</title>
		<link>http://www.andymcm.com/?p=20</link>
		<comments>http://www.andymcm.com/?p=20#comments</comments>
		<pubDate>Sun, 13 Dec 2009 14:28:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=20</guid>
		<description><![CDATA[Unity includes some basic AOP functionality.  One of the interception mechanisms it offers is VirtualMethodInterceptor, which works by building a derived class at runtime and overriding the virtual methods of the target class.  This approach has some obvious limitations, but it seemed like a good way to handle some simple logging &#38; profiling requirements that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codeplex.com/unity/">Unity</a> includes some basic AOP functionality.  One of the interception mechanisms it offers is VirtualMethodInterceptor, which works by building a derived class at runtime and overriding the virtual methods of the target class.  This approach has some obvious limitations, but it seemed like a good way to handle some simple logging &amp; profiling requirements that I have with my current project.</p>
<p>The first problem I hit is that the current release of Unity (1.2) has a huge bug &#8211; VirtualMethodInterceptor doesn’t work with classes that have parameters in their constructors.  Fortunately downloading and building the latest source was enough to get around that problem.</p>
<p>The next problem is that VirtualMethodInterceptor requires the target class to be public.  This is a common problem with code that builds derived classes at runtime.  <a href="http://code.google.com/p/moq/">Moq</a> has the same issue, for example.  The standard workaround is to use the InternalsVisibleTo assembly attribute to give the dynamic assembly access to internal classes in the target assembly.   So after a little digging in the source I found the name of the dynamic assembly, and added this to my assembly:</p>
<pre class="csharpcode">[assembly: InternalsVisibleTo(<span class="str">"Unity_ILEmit_DynamicClasses"</span>)]</pre>
<p>Unfortunately this is not sufficient.  The Unity code has some validation that insists on the target class being public.  The code is in VirtualMethodInterceptor.cs:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">bool</span> CanIntercept(Type t)
{
    Guard.ArgumentNotNull(t, <span class="str">"t"</span>);
    <span class="kwrd">return</span> t.IsClass &amp;&amp;
        (t.IsPublic || t.IsNestedPublic) &amp;&amp;
        t.IsVisible &amp;&amp;
        !t.IsSealed;
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->Removing the checks for IsPublic and IsVisible was enough to finally get everything to work correctly:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">bool</span> CanIntercept(Type t)
{
    Guard.ArgumentNotNull(t, <span class="str">"t"</span>);
    <span class="kwrd">return</span> t.IsClass &amp;&amp; !t.IsSealed;
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->Hopefully this will be fixed in time for the release of Unity 2.0.</p>
<p><strong>Update</strong>: For internal interfaces with InterfaceInterceptor, you need this:</p>
<pre class="csharpcode">[assembly: InternalsVisibleTo(<span class="str">"Unity_ILEmit_InterfaceProxies"</span>)]</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=20</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Managed threads in “whole stack committed” shocker</title>
		<link>http://www.andymcm.com/?p=19</link>
		<comments>http://www.andymcm.com/?p=19#comments</comments>
		<pubDate>Mon, 30 Nov 2009 21:28:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=19</guid>
		<description><![CDATA[I was experimenting with thread creation in a managed process, to test the limits, and I noticed something very odd.&#160; Creating a large number of threads was using a surprisingly large amount of memory.&#160; I guessed it must be the thread stacks, but the numbers didn’t fit with my understanding of how thread stacks are [...]]]></description>
			<content:encoded><![CDATA[<p>I was experimenting with thread creation in a managed process, to test the limits, and I noticed something very odd.&nbsp; Creating a large number of threads was using a surprisingly large amount of memory.&nbsp; I guessed it must be the thread stacks, but the numbers didn’t fit with my understanding of how thread stacks are allocated.&nbsp;&nbsp; </p>
<p>In an unmanaged process, by default each thread has 1 Mbyte of address space reserved for its stack, but initially only a small amount of this address space is committed.&nbsp;&nbsp; You can see this with the excellent <a href="http://technet.microsoft.com/en-us/sysinternals/dd535533.aspx">VMMAP</a> utility – here’s what it shows for an unmanaged x64 application:</p>
<p><a href="http://lh3.ggpht.com/_9JcejghnbMY/SxQ44bC1cOI/AAAAAAAAF0E/hpSqpjcsn5M/s1600-h/unmanaged_threads_x64%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="unmanaged_threads_x64" border="0" alt="unmanaged_threads_x64" src="http://lh4.ggpht.com/_9JcejghnbMY/SxQ44j1FRMI/AAAAAAAAF0I/2n-n884mQbs/unmanaged_threads_x64_thumb%5B3%5D.png?imgmax=800" width="415" height="102"></a> </p>
<p>The ‘size’ is the reserved address space – 1024k as expected.&nbsp; The committed memory is 16k, which is the amount of the address space that actually has physical storage associated with it.&nbsp;&nbsp; This can grow as required, up to the size of the reserved address space, but usually it won’t get close to that.&nbsp; </p>
<p>The size of the reserved address space is important in the sense that we need to make sure we don’t run out – 2000 threads would be enough to max out a 32-bit process, for example.&nbsp;&nbsp; But generally speaking it’s the committed size that has more impact &#8211; that number has to be backed by real storage, in RAM or at least the page file.</p>
<p>So, back to the managed process.&nbsp; This is what VMMAP shows:</p>
<p><a href="http://lh3.ggpht.com/_9JcejghnbMY/SxQ442H7EQI/AAAAAAAAF0M/PpWTkxcduXI/s1600-h/managed_threads_x64%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="managed_threads_x64" border="0" alt="managed_threads_x64" src="http://lh6.ggpht.com/_9JcejghnbMY/SxQ46DQ1fuI/AAAAAAAAF0Q/WKPx_YjLjOg/managed_threads_x64_thumb%5B1%5D.png?imgmax=800" width="405" height="96"></a> </p>
<p>Reserved address space is the same as before at 1024k, but the whole thing is committed!&nbsp; So each thread is using the full 1 Mbyte of physical storage, regardless of how much memory the stack actually needs.&nbsp;&nbsp; That can’t be right, can it?&nbsp;&nbsp; Well it seems it is, as <a href="http://www.bluebytesoftware.com/blog/2007/03/10/TheCLRCommitsTheWholeStack.aspx">Joe Duffy explains</a>.</p>
<p>So, the bottom line is that you should think carefully about creating managed threads that are going to sit idle in your process, for example in a thread pool.&nbsp;&nbsp; In an unmanaged process those idle threads wouldn’t be using many resources, but in a managed process they’re using a significant chunk of valuable memory.&nbsp;&nbsp; This is even more important if you have any form of multi-process architecture, perhaps with thread pools in each process.&nbsp; </p>
<p>If you really need a large number of long-lived threads in your managed process, consider reducing the size of the stack using the appropriate overload of the System.Threading.Thread constructor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=19</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improving performance of Parallel30.For</title>
		<link>http://www.andymcm.com/?p=18</link>
		<comments>http://www.andymcm.com/?p=18#comments</comments>
		<pubDate>Wed, 18 Nov 2009 00:17:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=18</guid>
		<description><![CDATA[In my last post I presented some code for a .NET 3.x version of the .NET 4.0 Parallel.For method.&#160;&#160; For many scenarios the performance of that Parallel30.For method is fine, but it performs very poorly when there are a large number of iterations and a small amount of cpu-bound work to do in each one.&#160;&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://www.andymcm.com/blog/2009/11/parallelfor-for-net-3x.html">post</a> I presented some code for a .NET 3.x version of the .NET 4.0 Parallel.For method.&nbsp;&nbsp; For many scenarios the performance of that Parallel30.For method is fine, but it performs very poorly when there are a large number of iterations and a small amount of cpu-bound work to do in each one.&nbsp;&nbsp; For example:</p>
<pre class="csharpcode"><span class="kwrd">int</span> size = 20000000;
<span class="kwrd">int</span>[] vector = <span class="kwrd">new</span> <span class="kwrd">int</span>[size];
Parallel.For(0, size, i =&gt; vector[i] = i % 100);</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Here is the performance comparison for three scenarios:&nbsp; 1) running the code&nbsp; in a standard (serial) for loop,&nbsp; 2) the .NET 4.0 Parallel.For,&nbsp; and 3) my Parallel30.For.&nbsp;&nbsp;&nbsp; The code is running on my dual-proc laptop.</p>
<p><strong>Serial</strong>:&nbsp; 400ms<br /><strong>.NET 4.0 Parallel.For</strong>:&nbsp; 280ms<br /><strong>My Parallel30.For</strong>:&nbsp; 8000ms</p>
<p>Ouch.&nbsp; My Parallel30.For is 20 times slower than a standard for loop, and nearly 30 times slower than .NET 4.0 Parallel.For.&nbsp;&nbsp; I suppose it’s really not very surprising when you consider that my code is calling ThreadPool.QueueUserWorkItem for every iteration – that’s 20 million times in this scenario.&nbsp;&nbsp; In some respects it’s impressive that it only takes eight seconds <img src='http://www.andymcm.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>So I wrote another version of the For method, which I’ve called CpuFor, targeted at this sort of scenario.&nbsp;&nbsp; Instead of submitting a separate task to the thread pool for every iteration, it divides the iterations into ranges based on the processor count, then submits each range as a task to the thread pool.&nbsp;&nbsp; So instead of 20 million tasks, each executing one iteration, we’ll get 2 tasks, each executing 10 million iterations.&nbsp; Or on a quad-proc machine, 4 tasks each executing 5 million iterations.&nbsp;&nbsp; Of course this assumes that on average each iteration takes a roughly equal amount of time, but that’s probably mostly true for this type of scenario.</p>
<p>The results are pretty good:</p>
<p><strong>My Parallel30.CpuFor</strong>: 250ms</p>
<p>I’m now (just about) beating .NET 4.0 Parallel.For.&nbsp;&nbsp; Though I had to go pretty specialised to do it, which makes the .NET 4.0 Parallel.For all the more impressive.&nbsp;&nbsp; I must crack it open with Reflector some time <img src='http://www.andymcm.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Anyway, here’s the code:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> CpuFor(<span class="kwrd">int</span> fromInclusive, <span class="kwrd">int</span> toExclusive, Action&lt;<span class="kwrd">int</span>&gt; action)
{
    <span class="kwrd">using</span> (var doneEvent = <span class="kwrd">new</span> ManualResetEvent(<span class="kwrd">false</span>))
    {
        <span class="kwrd">int</span> iterations = (toExclusive - fromInclusive);
        <span class="kwrd">int</span> iterationsCompleted = 0;
        Exception actionEx = <span class="kwrd">null</span>;
        <span class="kwrd">int</span> processors = Environment.ProcessorCount;

        WaitCallback iterationCode =
            (arg) =&gt;
            {
                var range = (KeyValuePair&lt;<span class="kwrd">int</span>, <span class="kwrd">int</span>&gt;)arg;
                <span class="kwrd">try</span>
                {
                    <span class="kwrd">int</span> from = range.Key;
                    <span class="kwrd">int</span> to = range.Value;
                    <span class="kwrd">for</span> (<span class="kwrd">int</span> i = from; i &lt; to; i++)
                        action(i);
                }
                <span class="kwrd">catch</span> (Exception ex)
                {
                    actionEx = ex;
                }

                <span class="kwrd">int</span> completed = Interlocked.Add(<span class="kwrd">ref</span> iterationsCompleted, range.Value - range.Key);
                <span class="kwrd">if</span> (completed == iterations)
                    doneEvent.Set();
            };

        <span class="kwrd">int</span> rangeSize = Math.Max(1, iterations / processors);
        <span class="kwrd">for</span> (<span class="kwrd">int</span> i = fromInclusive; i &lt; toExclusive; i += rangeSize)
        {
            <span class="kwrd">int</span> from = i;
            <span class="kwrd">int</span> to = Math.Min(i + rangeSize, toExclusive);
            var range = <span class="kwrd">new</span> KeyValuePair&lt;<span class="kwrd">int</span>, <span class="kwrd">int</span>&gt;(from, to);
            ThreadPool.QueueUserWorkItem(iterationCode, range);
        }

        doneEvent.WaitOne();

        <span class="kwrd">if</span> (actionEx != <span class="kwrd">null</span>)
            <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(<span class="str">"Action failed"</span>, actionEx);
    }
}</pre>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
</p>
<p>BTW If you think that the delegate invocation – action(i) – on every iteration must be a performance killer, it doesn’t seem to be.&nbsp;&nbsp; I wrote another version that eliminates the action() call (at the expense of usability) but the performance improvement was marginal.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=18</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel.For for .NET 3.x</title>
		<link>http://www.andymcm.com/?p=17</link>
		<comments>http://www.andymcm.com/?p=17#comments</comments>
		<pubDate>Thu, 12 Nov 2009 21:46:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=17</guid>
		<description><![CDATA[.NET 4.0 includes an interesting class called Parallel, with a For method that allows the iterations of a for loop to be executed concurrently, like this: Parallel.For(0, 10, i =&#62; Console.WriteLine(i)); .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; [...]]]></description>
			<content:encoded><![CDATA[<p>.NET 4.0 includes an interesting class called Parallel, with a For method that allows the iterations of a for loop to be executed concurrently, like this:</p>
<pre class="csharpcode">Parallel.For(0, 10, i =&gt; Console.WriteLine(i));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>I’ve done a lot of multithreading over the years, but I must confess it’s never occurred to me to apply concurrency in this way, at the level of a simple for loop.&nbsp;&nbsp; I’m not sure how often I’ll use it in practice, but my current work project is .NET 3.5 so I wanted to have a .NET 3.x version of Parallel.For to play with.&nbsp;&nbsp; The code is shown below.&nbsp; </p>
<p>Some basic tests reveal that at the extremes the performance is not as good as the .NET 4.0 version, but for more common scenarios it seems to be comparable.&nbsp;&nbsp; </p>
<pre class="csharpcode"><span class="kwrd">static</span> <span class="kwrd">class</span> Parallel30
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> For(<span class="kwrd">int</span> fromInclusive, <span class="kwrd">int</span> toExclusive, Action&lt;<span class="kwrd">int</span>&gt; action)
    {
        <span class="kwrd">using</span> (var doneEvent = <span class="kwrd">new</span> ManualResetEvent(<span class="kwrd">false</span>))
        {
            <span class="kwrd">int</span> iterations = (toExclusive - fromInclusive);
            <span class="kwrd">int</span> iterationsCompleted = 0;
            Exception actionEx = <span class="kwrd">null</span>;

            WaitCallback iterationCode =
                (arg) =&gt;
                {
                    <span class="kwrd">try</span>
                    {
                        action((<span class="kwrd">int</span>)arg);
                    }
                    <span class="kwrd">catch</span> (Exception ex)
                    {
                        actionEx = ex;
                    }

                    <span class="kwrd">int</span> completed = Interlocked.Increment(<span class="kwrd">ref</span> iterationsCompleted);
                    <span class="kwrd">if</span> (completed == iterations)
                        doneEvent.Set();
                };

            <span class="kwrd">for</span> (<span class="kwrd">int</span> i = fromInclusive; i &lt; toExclusive; i++)
            {
                ThreadPool.QueueUserWorkItem(iterationCode, i);
            }

            doneEvent.WaitOne();

            <span class="kwrd">if</span> (actionEx != <span class="kwrd">null</span>)
                <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(<span class="str">"Action failed"</span>, actionEx);
        }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=17</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity and primitive constructor arguments</title>
		<link>http://www.andymcm.com/?p=16</link>
		<comments>http://www.andymcm.com/?p=16#comments</comments>
		<pubDate>Tue, 10 Nov 2009 21:37:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IOC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=16</guid>
		<description><![CDATA[I’m using the Unity IoC container for the first time, and overall I like it.&#160; But one under-documented and slightly unintuitive area caused me some trouble. Suppose I have a ThreadPool class: class ThreadPool { public ThreadPool(int maxThreads) { ... } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, [...]]]></description>
			<content:encoded><![CDATA[<p>I’m using the Unity IoC container for the first time, and overall I like it.&nbsp; But one under-documented and slightly unintuitive area caused me some trouble.</p>
<p>Suppose I have a ThreadPool class:</p>
<pre class="csharpcode"><span class="kwrd">class</span> ThreadPool
{
   <span class="kwrd">public</span> ThreadPool(<span class="kwrd">int</span> maxThreads)
   {
      ...
   }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>I can register this in the container as follows:</p>
<pre class="csharpcode">container.RegisterType&lt;ThreadPool&gt;(<span class="kwrd">new</span> InjectionConstructor(20));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>So every time the container creates an instance of ThreadPool, it will pass 20 for the maxThreads argument.&nbsp;&nbsp; It’s not the most intuitive API, but it does the job.</p>
<p>However suppose I now extend my ThreadPool class to add logging, i.e.:</p>
<pre class="csharpcode"><span class="kwrd">class</span> ThreadPool
{
   <span class="kwrd">public</span> ThreadPool(<span class="kwrd">int</span> maxThreads, ILogger logger)
   {
      ...
   }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>How do I modify my container registration to inject the correct ILogger instance?&nbsp; Of course I could just create a Logger instance and pass it as an extra argument to InjectorConstructor, but that’s not the behaviour I want.&nbsp;&nbsp; I want the container to resolve ILogger when the ThreadPool instance is being resolved.&nbsp;&nbsp; Having looked through the documentation, I was coming to the conclusion that this wasn’t possible, but then I came across <a href="http://www.pnpguidance.net/post/UnityStructureMapDynamicallyConfiguringConstructorInjectionFluentInterfaces.aspx">this post</a> on pnpguidance.net that shows how it’s done:</p>
<pre class="csharpcode">container.RegisterType&lt;ThreadPool&gt;(<span class="kwrd">new</span> InjectionConstructor(20,<span class="kwrd">typeof</span>(ILogger)));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>I suppose using typeof in this scenario makes some sense, but it’s hardly intuitive.&nbsp; The API could be improved so that I don’t need to specify anything about ILogger resolution at all.&nbsp;&nbsp; I should only have to specify explicitly the constructor arguments that cannot be resolved automatically, e.g.:</p>
<pre class="csharpcode">container.RegisterType&lt;ThreadPool&gt;().WithConstructorArg(<span class="str">"maxThreads"</span>, 20);</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=16</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managed DCOM server</title>
		<link>http://www.andymcm.com/?p=15</link>
		<comments>http://www.andymcm.com/?p=15#comments</comments>
		<pubDate>Sat, 24 Oct 2009 10:53:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[DCOM]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WWSAPI]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=15</guid>
		<description><![CDATA[For my current project I have some old unmanaged C++ code that needs to talk to a new .NET server, remotely.   The channel needs to be fast and secure.   There are a lot of options, but I’ve narrowed it down to two: WWSAPI for the C++ client, WCF for the .NET server, using NetTcpBinding.  (See [...]]]></description>
			<content:encoded><![CDATA[<p>For my current project I have some old unmanaged C++ code that needs to talk to a new .NET server, remotely.   The channel needs to be fast and secure.   There are a lot of options, but I’ve narrowed it down to two:</p>
<ol>
<li>WWSAPI for the C++ client, WCF for the .NET server, using NetTcpBinding.  (See <a href="http://blogs.msdn.com/haoxu/archive/2008/12/02/wwsapi-to-wcf-interop-nettcpbinding-with-transport-security.aspx">here</a> for details.)</li>
<li>DCOM</li>
</ol>
<p>Option 1 was the strong favourite until Microsoft decided to play <a href="http://blogs.msdn.com/wndp/archive/2009/10/09/final-version-of-windows-web-services-api-for-windows-xp-vista-server-2003-and-server-2008-is-now-available.aspx">silly buggers</a> with the redistribution rights for WWSAPI, so I’ve been forced to investigate option 2.</p>
<p>COM interop through loading .NET components into an unmanaged process is well documented and generally works fine, but accessing a .NET server remotely via (D)COM is not so well documented, and indeed it’s not even clear if it’s supported by Microsoft.   But it does seem to work, and it’s surprisingly simple once you discover the RegisterTypeForComClients method on the RegistrationServices class.   This is basically a wrapper for COM’s CoRegisterClassObject.</p>
<p>Here’s the C# server code:</p>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<pre class="csharpcode">[ComVisible(<span class="kwrd">true</span>)]
<span class="kwrd">public</span> <span class="kwrd">interface</span> ICalculator
{
    <span class="kwrd">int</span> Add(<span class="kwrd">int</span> x, <span class="kwrd">int</span> y);
}

[ComVisible(<span class="kwrd">true</span>)]
[ClassInterface(ClassInterfaceType.None)]
<span class="kwrd">public</span> <span class="kwrd">class</span> Calculator : ICalculator
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Add(<span class="kwrd">int</span> x, <span class="kwrd">int</span> y) { <span class="kwrd">return</span> x + y; }
}

<span class="kwrd">class</span> Program
{
    [MTAThread]
    <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
    {
        var regServices = <span class="kwrd">new</span> RegistrationServices();

        <span class="kwrd">int</span> cookie = regServices.RegisterTypeForComClients(
            <span class="kwrd">typeof</span>(Calculator),
            RegistrationClassContext.LocalServer | RegistrationClassContext.RemoteServer,
            RegistrationConnectionType.MultipleUse);

        Console.WriteLine(<span class="str">"Ready"</span>); Console.ReadKey();

        regServices.UnregisterTypeForComClients(cookie);
    }
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->and the C++ client code:</p>
<pre class="csharpcode">#import <span class="str">"dcomserver.tlb"</span> no_namespace raw_interfaces_only

<span class="kwrd">int</span> _tmain(<span class="kwrd">int</span> argc, _TCHAR* argv[])
{
    CoInitializeEx(0, COINIT_MULTITHREADED);

    {
        CComPtr&lt;ICalculator&gt; spCalc;
        spCalc.CoCreateInstance(__uuidof(Calculator), 0, CLSCTX_LOCAL_SERVER);

        <span class="kwrd">long</span> result = 0;
        spCalc-&gt;Add(10, 20, &amp;result);

        cout &lt;&lt; result &lt;&lt; endl;
    }

    CoUninitialize();
    <span class="kwrd">return</span> 0;
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->(The simplest way to generate the tlb file to #import is to run regasm /tlb DcomServer.exe)</p>
<p>There may well be some gotchas with this approach – I haven’t tested it thoroughly yet – but it seems a promising option if the WWSAPI licensing issues can’t be sorted out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=15</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WCF and abstract types</title>
		<link>http://www.andymcm.com/?p=14</link>
		<comments>http://www.andymcm.com/?p=14#comments</comments>
		<pubDate>Mon, 12 Oct 2009 21:56:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=14</guid>
		<description><![CDATA[This seems like a fairly straightforward issue to me, but after googling around I’m left wondering what I’m missing. Suppose I have a web service that contains a Search() method that takes one of two possible types of query – a simple query or a complex query.&#160;&#160; WCF doesn’t support xsd:choice, so I can’t use [...]]]></description>
			<content:encoded><![CDATA[<p>This seems like a fairly straightforward issue to me, but after googling around I’m left wondering what I’m missing.</p>
<p>Suppose I have a web service that contains a Search() method that takes one of two possible types of query – a simple query or a complex query.&nbsp;&nbsp; WCF doesn’t support xsd:choice, so I can’t use that, but it does support xsd:extension, so that should do the trick:</p>
<pre class="csharpcode">    [DataContract]
    [KnownType(<span class="kwrd">typeof</span>(SimpleQuery))]
    [KnownType(<span class="kwrd">typeof</span>(ComplexQuery))]
    <span class="kwrd">class</span> Query { }

    [DataContract]
    <span class="kwrd">class</span> SimpleQuery : Query { }

    [DataContract]
    <span class="kwrd">class</span> ComplexQuery : Query { }

    [ServiceContract]
    <span class="kwrd">interface</span> ISearchService
    {
        Results Search(Query query);
    }</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>This works ok, but it’s not quite right – the client can pass an instance of Query rather than SimpleQuery or ComplexQuery.&nbsp;&nbsp; Query really needs to be abstract.&nbsp;&nbsp; But does XML schema support that?&nbsp;&nbsp; Yes, it does, through complextype’s abstract attribute.</p>
<p>So, problem solved.&nbsp; We make Query abstract and WCF’s DataContractSerializer will add the abstract=”true” to the XSD it generates.&nbsp; Except that it doesn’t.&nbsp; It just ignores the fact that the Query class is abstract, which means that the client sees the class as non-abstract.&nbsp; This means that the client can easily pass a Query instance to the server (which of course&nbsp; causes the serializer on the server to blow up trying to create an instance of the abstract class).&nbsp;&nbsp; Hmm.</p>
<p>So why doesn’t DataContractSerializer support abstract base classes?&nbsp; It seems pretty simple, both in concept and implementation.&nbsp;&nbsp; Presumably it’s deemed too OO-centric … but then why support inheritance (xsd:extension)?&nbsp;&nbsp; </p>
<p>BTW There’s a StackOverflow <a href="http://stackoverflow.com/questions/839696/why-doesnt-wcf-properly-consume-expose-abstract-types-when-hosted-as-a-web-ser">question</a> covering the same issue – like the poster I didn’t find any of the answers satisfactory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=14</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF Toolkit DataGrid is an uggo</title>
		<link>http://www.andymcm.com/?p=13</link>
		<comments>http://www.andymcm.com/?p=13#comments</comments>
		<pubDate>Fri, 02 Oct 2009 21:49:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=13</guid>
		<description><![CDATA[Update 6-Nov-09:&#160; The style given here works fine for the WPF 4.0 DataGrid (beta 2). This is the default look for the WPF Toolkit DataGrid: Ugh.&#160; The black gridlines, the white-on-blue highlight, the cramped text … it’s really not very good.&#160;&#160; I’m no graphic designer, but I think the following simple tweaks are a significant [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update 6-Nov-09</strong>:&nbsp; The style given here works fine for the WPF 4.0 DataGrid (beta 2).</em></p>
<p>This is the default look for the WPF Toolkit DataGrid:</p>
<p><a href="http://lh3.ggpht.com/_9JcejghnbMY/SsZ1etHCBlI/AAAAAAAAFhM/63hqNcJ0CIg/s1600-h/datagrid1%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="datagrid1" border="0" alt="datagrid1" src="http://lh6.ggpht.com/_9JcejghnbMY/SsZ1fPlPDmI/AAAAAAAAFhQ/uQeAibnFurQ/datagrid1_thumb%5B3%5D.png?imgmax=800" width="698" height="131"></a></p>
<p>Ugh.&nbsp; The black gridlines, the white-on-blue highlight, the cramped text … it’s really not very good.&nbsp;&nbsp; I’m no graphic designer, but I think the following simple tweaks are a significant improvement:</p>
<p><a href="http://lh4.ggpht.com/_9JcejghnbMY/SsZ1fx9icbI/AAAAAAAAFhU/UJOvhh1cC38/s1600-h/datagrid2%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="datagrid2" border="0" alt="datagrid2" src="http://lh6.ggpht.com/_9JcejghnbMY/SsZ1gfb66II/AAAAAAAAFhY/YmyUV6MUc-I/datagrid2_thumb%5B3%5D.png?imgmax=800" width="697" height="151"></a>&nbsp; </p>
<p>The gridlines are trivial to change through the DataGrid’s HorizontalGridLinesBrush and VerticalGridLinesBrush properties.&nbsp;&nbsp; The selection highlight can be modified by setting the CellStyle property to a style containing a trigger on the IsSelected property of the DataGridCell.&nbsp; Finally, I added the padding by defining a control template for the cell that has a padded border around the ContentPresenter.&nbsp;&nbsp; As a little extra I also highlight a row when the mouse passes over it, which I find pleasing to the eye.</p>
<p>Here’s the complete style:</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Style</span> <span class="attr">x:Key</span><span class="kwrd">="PrettierDataGridStyle"</span> <span class="attr">TargetType</span><span class="kwrd">="tk:DataGrid"</span><span class="kwrd">&gt;</span>

    <span class="rem">&lt;!-- Make the border and grid lines a little less imposing --&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="BorderBrush"</span> <span class="attr">Value</span><span class="kwrd">="#DDDDDD"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="HorizontalGridLinesBrush"</span> <span class="attr">Value</span><span class="kwrd">="#DDDDDD"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="VerticalGridLinesBrush"</span> <span class="attr">Value</span><span class="kwrd">="#DDDDDD"</span> <span class="kwrd">/&gt;</span>

    <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="RowStyle"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">Style</span> <span class="attr">TargetType</span><span class="kwrd">="tk:DataGridRow"</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Style.Triggers</span><span class="kwrd">&gt;</span>
                    <span class="rem">&lt;!-- Highlight a grid row as the mouse passes over --&gt;</span>
                    <span class="kwrd">&lt;</span><span class="html">Trigger</span> <span class="attr">Property</span><span class="kwrd">="IsMouseOver"</span> <span class="attr">Value</span><span class="kwrd">="True"</span><span class="kwrd">&gt;</span>
                        <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="Background"</span> <span class="attr">Value</span><span class="kwrd">="Lavender"</span> <span class="kwrd">/&gt;</span>
                    <span class="kwrd">&lt;/</span><span class="html">Trigger</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;/</span><span class="html">Style.Triggers</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;/</span><span class="html">Style</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Setter</span><span class="kwrd">&gt;</span>

    <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="CellStyle"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">Style</span> <span class="attr">TargetType</span><span class="kwrd">="tk:DataGridCell"</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Style.Triggers</span><span class="kwrd">&gt;</span>
                    <span class="rem">&lt;!-- Highlight selected rows --&gt;</span>
                    <span class="kwrd">&lt;</span><span class="html">Trigger</span> <span class="attr">Property</span><span class="kwrd">="IsSelected"</span> <span class="attr">Value</span><span class="kwrd">="True"</span><span class="kwrd">&gt;</span>
                        <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="Background"</span> <span class="attr">Value</span><span class="kwrd">="Lavender"</span> <span class="kwrd">/&gt;</span>
                        <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="BorderBrush"</span> <span class="attr">Value</span><span class="kwrd">="Lavender"</span> <span class="kwrd">/&gt;</span>
                        <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="Foreground"</span> <span class="attr">Value</span><span class="kwrd">="Black"</span> <span class="kwrd">/&gt;</span>
                    <span class="kwrd">&lt;/</span><span class="html">Trigger</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;/</span><span class="html">Style.Triggers</span><span class="kwrd">&gt;</span>

                <span class="rem">&lt;!-- Add some padding around the contents of a cell --&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="Padding"</span> <span class="attr">Value</span><span class="kwrd">="4,3,4,3"</span> <span class="kwrd">/&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">Setter</span> <span class="attr">Property</span><span class="kwrd">="Template"</span><span class="kwrd">&gt;</span>
                    <span class="kwrd">&lt;</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
                        <span class="kwrd">&lt;</span><span class="html">ControlTemplate</span> <span class="attr">TargetType</span><span class="kwrd">="tk:DataGridCell"</span><span class="kwrd">&gt;</span>
                            <span class="kwrd">&lt;</span><span class="html">Border</span> <span class="attr">Padding</span><span class="kwrd">="{TemplateBinding Padding}"</span>
                                <span class="attr">Background</span><span class="kwrd">="{TemplateBinding Background}"</span><span class="kwrd">&gt;</span>
                                <span class="kwrd">&lt;</span><span class="html">ContentPresenter</span> <span class="kwrd">/&gt;</span>
                            <span class="kwrd">&lt;/</span><span class="html">Border</span><span class="kwrd">&gt;</span>
                        <span class="kwrd">&lt;/</span><span class="html">ControlTemplate</span><span class="kwrd">&gt;</span>
                    <span class="kwrd">&lt;/</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;/</span><span class="html">Setter</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;/</span><span class="html">Style</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">Setter.Value</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Setter</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Style</span><span class="kwrd">&gt;</span>
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=13</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.NET FAQ is no more</title>
		<link>http://www.andymcm.com/?p=12</link>
		<comments>http://www.andymcm.com/?p=12#comments</comments>
		<pubDate>Thu, 01 Oct 2009 20:05:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[FAQ]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=12</guid>
		<description><![CDATA[I was considering an overhaul of my two FAQs, as I haven&#8217;t really done any work on them for years.&#160;&#160; But looking through the .NET FAQ, it became obvious that it would need a total rewrite, and there doesn&#8217;t seem much point, with all the books and web resources available now.&#160; It served its purpose [...]]]></description>
			<content:encoded><![CDATA[<p>I was considering an overhaul of my two FAQs, as I haven&#8217;t really done any work on them for years.&nbsp;&nbsp; But looking through the .NET FAQ, it became obvious that it would need a total rewrite, and there doesn&#8217;t seem much point, with all the books and web resources available now.&nbsp; It served its purpose in the early .NET days (the FAQ was first published in 2000!), but it&#8217;s time for it to be retired.&nbsp; </p>
<p>I&#8217;ll leave the document on the site, so bookmarks will still work, but I&#8217;ve removed all links to it from my home page.&nbsp; Interesting to see how quickly that affects the search engine ranking &#8211; after all these years it&#8217;s still number one for &#8216;.net faq&#8217;, in both google and bing.</p>
<p>The C# FAQ (for C++ programmers) looks a bit more salvageable, so I&#8217;ll keep it for now, and aim to give it an overhaul over the next few months.&nbsp;&nbsp; </p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=12</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building lambda expressions at run time</title>
		<link>http://www.andymcm.com/?p=11</link>
		<comments>http://www.andymcm.com/?p=11#comments</comments>
		<pubDate>Wed, 23 Sep 2009 19:26:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=11</guid>
		<description><![CDATA[Moq is a great library for creating fake objects for use in unit tests. It makes heavy use of lambda expressions to configure the mock, and also to verify that operations have been performed on the mock. For example, suppose we want to verify that the Insert method on an ICache interface has been called: [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/moq/">Moq</a> is a great library for creating fake objects for use in unit tests. It makes heavy use of lambda expressions to configure the mock, and also to verify that operations have been performed on the mock. For example, suppose we want to verify that the Insert method on an ICache interface has been called:</p>
<pre class="csharpcode"><span class="rem">// Arrange </span>
var mockCache = <span class="kwrd">new</span> Mock&lt;ICache&gt;();
var someObject = <span class="kwrd">new</span> <span class="kwrd">object</span>();

<span class="rem">// Act </span>
DoSomethingThatShouldAddObjectToCache(mockCache, someObject);

<span class="rem">// Assert </span>
mockCalc.Verify( cache =&gt; cache.Insert(someObject) );</pre>
<p>The key thing here is the use of a lambda expression in the Verify method. The lambda expression is just a convenient way to create a data structure specifying the method call we’re interested in (ICache.Insert) and the arguments we expect it to be called with (someObject). The Verify method is not going to <em>execute</em> the Insert method call – it’s merely going to examine the lambda expression structure to determine whether it matches a previous call on the mock (which the mock recorded in case we wanted to perform this type of verification).</p>
<p>The use of lambda expressions to package method names and arguments is a concise, typesafe and intellisense-friendly approach, but at first glance it appears to limit dynamic usage at run-time. An <a href="http://groups.google.com/group/moqdisc/browse_thread/thread/affaf64c980d16cc">example</a> of this problem was recently posted to the <a href="http://groups.google.com/group/moqdisc">Moq discussion forum</a> – the poster wanted to call mock.VerifyGet for every property on an interface, without having to write a VerifyGet call for every property individually.</p>
<p>Obviously reflection can be used to get the properties at runtime, but how do we build the appropriate lambda expression to pass to mock.VerifyGet()? Well, it turns out to be quite straightforward, using the LambaExpression class. The following code builds an expression of the type p =&gt; p.Prop, which is what we need for VerifyGet: </p>
<pre class="csharpcode">ParameterExpression p = Expression.Parameter(mockType, <span class="str">"p"</span>);
MemberExpression body = Expression.Property(p, prop);
LambdaExpression expr = Expression.Lambda(body, p);</pre>
<p>Wrap this up in an extension method for the Mock&lt;T&gt; class, and we get the following VerifyAllGets() method: </p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> VerifyAllGets&lt;T&gt;(<span class="kwrd">this</span> Mock&lt;T&gt; mock) <span class="kwrd">where</span> T : <span class="kwrd">class</span>
{
    <span class="rem">// Get a MethodInfo for the Mock&lt;T&gt;.VerifyGet&lt;TProp&gt; method.</span>
    MethodInfo verifyGetMethod = <span class="kwrd">typeof</span>(Mock&lt;T&gt;).GetMethods(
        BindingFlags.Public | BindingFlags.Instance).Where(
        mi =&gt; mi.Name == <span class="str">"VerifyGet"</span> &amp;&amp;
        mi.GetParameters().Length == 1).First();

    <span class="rem">// Call the Mock&lt;T&gt;.VerifyGet method separately for each property on T.</span>
    <span class="kwrd">foreach</span> (var prop <span class="kwrd">in</span> <span class="kwrd">typeof</span>(T).GetProperties())
    {
        <span class="rem">// Build a lambda expression of the form p =&gt; p.Prop</span>
        ParameterExpression p = Expression.Parameter(<span class="kwrd">typeof</span>(T), <span class="str">"p"</span>);
        MemberExpression body = LambdaExpression.Property(p, prop);
        LambdaExpression expr = LambdaExpression.Lambda(body, p);

        <span class="rem">// Mock&lt;T&gt;.VerifyGet&lt;TProp&gt; must be 'closed' by specifying TProp</span>
        var closedVerifyGetMethod = verifyGetMethod.MakeGenericMethod(prop.PropertyType);

        <span class="rem">// Call Mock&lt;T&gt;.VerifyGet(expr)</span>
        closedVerifyGetMethod.Invoke(mock, <span class="kwrd">new</span> <span class="kwrd">object</span>[] { expr });
    }
}</pre>
<p>The VerifyAllGets() method can be used as follows:</p>
<pre class="csharpcode"><span class="rem">// Arrange</span>
var mock = <span class="kwrd">new</span> Mock&lt;IManyProps&gt;();

<span class="rem">// Act</span>
RunCodeThatGetsAllProps(mock);

<span class="rem">// Assert</span>
mock.VerifyAllGets();</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some cool new(-ish) NUnit features</title>
		<link>http://www.andymcm.com/?p=8</link>
		<comments>http://www.andymcm.com/?p=8#comments</comments>
		<pubDate>Wed, 18 Mar 2009 23:07:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=8</guid>
		<description><![CDATA[I haven&#8217;t looked at NUnit for a long time, and things have moved on a lot since I last used it. I particularly like the support for parameterised tests. For example, the TestCase attribute can be used to specify multiple iterations for a particular test, e.g.: [TestCase(CalculatorMode.Standard)] [TestCase(CalculatorMode.Scientific)] public void TestAdd(CalculatorMode mode) { Calculator calc [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t looked at NUnit for a long time, and things have moved on a lot since I last used it.  I particularly like the support for parameterised tests.  For example, the TestCase attribute can be used to specify multiple iterations for a particular test, e.g.:</p>
<pre>[TestCase(CalculatorMode.Standard)]
[TestCase(CalculatorMode.Scientific)]
public void TestAdd(CalculatorMode mode)
{
   Calculator calc = new Calculator(mode);
   int res = calc.Add(10,20);
   Assert.That(res, Is.EqualTo(30));
}</pre>
<p>This test will be executed twice, once with the calculator in &#8216;standard&#8217; mode, and once with the calculator in &#8216;scientific&#8217; mode.</p>
<p>We can take this a step further and use the Values and Range attributes to expand our set of tests:</p>
<pre>[Test]
public void TestAdd(
   [Values(CalculatorMode.Standard, CalculatorMode.Scientific)] CalculatorMode mode,
   [Range(0,5,1)] int x,
   [Range(0,5,1)] int y
   )
{
   Calculator calc = new Calculator(mode);
   int res = calc.Add(x,y);
   Assert.That(res, Is.EqualTo(x+y));
}</pre>
<p>This test will run a total of 72 times.  The range of x and y are both 0 to 5, with an interval of 1 (i.e. the values 0,1,2,3,4,5), giving 6&#215;6=36 tests, which are then combined with our two modes to give a total of 72 iterations.</p>
<p>The multiple iterations show up as separate tests in your test runner, with the names derived from the arguments used for each run, so it&#8217;s very easy to see what&#8217;s going on.</p>
<p>Also you may have noticed the constraint-based Assert syntax used in the examples above.  I was a little unsure of it at first, but it&#8217;s growing on me.  It uses a &#8216;fluent&#8217; style to make the Assert more readable, so:</p>
<pre>Assert.That(res, Is.EqualTo(x+y));</pre>
<p>is very close to the English &#8220;Assert that res is equal to x+y&#8221;.   There is a lot of debate about the merits of fluent APIs, but in this context I like it.</p>
<p>One constraint that really caught my eye is &#8216;After&#8217;.  It&#8217;s useful when testing asynchronous code where you need to wait for a condition to become true.  For example:</p>
<pre>Assert.That(() => Status, Is.EqualTo("Done").IgnoreCase.After(5000, 50));</pre>
<p>What this says is &#8220;Assert that status is &#8216;done&#8217; (case insensitive) after a maximum of 5 seconds, checking every 50 ms&#8221;.   The chaining of constraints like this is very readable, and the fact that relatively sophisticated constraints like After are built-in is very helpful.   (I actually wrote my own version of this asynchronous wait/poll functionality recently, but I can throw it away now and use After instead.)</p>
<p>Finally if you derive from the AssertionHelper class, you can cut down the verbosity slightly, and re-write this Assert as:</p>
<pre>Expect(() => Status, EqualTo("Done").IgnoreCase.After(5000,50));</pre>
<p>Let me know if you&#8217;ve found any other cool NUnit features that I might have missed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=8</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Given up on Vista</title>
		<link>http://www.andymcm.com/?p=7</link>
		<comments>http://www.andymcm.com/?p=7#comments</comments>
		<pubDate>Sat, 10 Mar 2007 18:52:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=7</guid>
		<description><![CDATA[So after a couple of months using Vista on my new laptop, I&#8217;ve given up and gone back to XP. The last straw was when Vista wouldn&#8217;t recognise my new digital camera, even though it&#8217;s just a standard USB disk device. After an hour or so of futile poking around in device manager, I finally [...]]]></description>
			<content:encoded><![CDATA[<p>So after a couple of months using Vista on my new laptop, I&#8217;ve given up and gone back to XP.  The last straw was when Vista wouldn&#8217;t recognise my new digital camera, even though it&#8217;s just a standard USB disk device.   After an hour or so of futile poking around in device manager, I finally did a google search and someone recommended telling Vista to look in c:\windows\system32 for the drivers it needs.  Incredibly that worked, but by that time I was so furious I made up my mind to reinstall XP the next night.</p>
<p>It&#8217;s *so* good to be back on XP.   Everything is faster, my disk isn&#8217;t constantly churning, my sound works properly (no stuttering), I can login using my fingerprint reader again,  and Windows Explorer works properly.   The only things I miss are the sidebar (solved by installing <a href="http://www.desktopsidebar.com/">desktop sidebar</a>), and the search in the start menu (which I can easily live without).</p>
<p>Don&#8217;t get me wrong, Vista is not terrible.  It&#8217;s just that for me on this particular laptop, the pain outweighs the gain.   Maybe I&#8217;ll try it again in a year or two, when we&#8217;ve had a couple of service packs and the drivers are all sorted out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First impressions of Zune</title>
		<link>http://www.andymcm.com/?p=6</link>
		<comments>http://www.andymcm.com/?p=6#comments</comments>
		<pubDate>Tue, 09 Jan 2007 14:34:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=6</guid>
		<description><![CDATA[Got a free Microsoft Zune as part of a promotion related to my website, so I thought I&#8217;d post some thoughts. Overall I have to say it&#8217;s a big disappointment. First of all, you can&#8217;t use it as an external drive, so you can&#8217;t store abitrary data on it. There is a hacky workaround circulating [...]]]></description>
			<content:encoded><![CDATA[<p>Got a free Microsoft Zune as part of a promotion related to my website, so I thought I&#8217;d post some thoughts.</p>
<p>Overall I have to say it&#8217;s a big disappointment.  First of all, you can&#8217;t use it as an external drive, so you can&#8217;t store abitrary data on it.  There is a hacky workaround circulating on the net, but it&#8217;s very kludgy. This limitation alone means I&#8217;d never recommend anyone buy one. </p>
<p>The second problem is the PC software (to sync your media with the Zune).  The interface is truly awful.  I found myself having to consult the help to work out how to do the most basic things.  And the help isn&#8217;t much good either.  Explaining this interface to a non-technical relative doesn&#8217;t bear thinking about. So think twice before buying one as a gift for someone.</p>
<p>I haven&#8217;t bothered signing up for the online marketplace stuff, so I can&#8217;t comment on that. But I read bad things about it, so I&#8217;m not really tempted to try.</p>
<p>As far the hardware goes, the screen is excellent &#8211; big and bright and very usable for looking at photos or even video.  The controls are reasonably intuitive. The earphones are mediocre &#8211; not close to the quality of my 30 quid Sennheiser PX100 headphones. With decent headphones, the sound quality from the Zune seems fine &#8211; similar to my PC.</p>
<p>The software on the Zune itself is ok, but not great &#8211; the built-in themes are ugly (subjective, obviously), skipping between songs is slow, and the equalizer only has presets (no direct control over bass and treble).  Oddly there seems to be hardly any noticable difference between the preset values &#8211; Rock has slightly more bass than Folk, for example, but it&#8217;s only noticeable at very high volumes.</p>
<p>Add in the fact that there&#8217;s no AC charger (I&#8217;m expected to take my laptop with me when travelling, obviously) and the whole Zune experience leaves a lot to be desired. I really can&#8217;t see myself using it.  Probably end up on ebay.  Along with quite a few others, I imagine.</p>
<p>BTW I have no experience with any other similar devices (e.g. IPod), so maybe they all suffer from similar problems.  I doubt it, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=6</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My MSDN bug has disappeared</title>
		<link>http://www.andymcm.com/?p=5</link>
		<comments>http://www.andymcm.com/?p=5#comments</comments>
		<pubDate>Tue, 15 Nov 2005 15:41:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=5</guid>
		<description><![CDATA[Logged into the MSDN feedback center today, to see the progress of the ListView bug I reported (see previous blog entry). To my surprise all record of the bug has disappeared. So I used a direct link to the bug, and I got this: Page is hidden This feedback is not being displayed because it [...]]]></description>
			<content:encoded><![CDATA[<p>Logged into the MSDN feedback center today, to see the progress of the ListView bug I reported (see <a href="http://www.andymcm.com/blog/2005/11/net-20-listview-virtualmode.html">previous blog entry</a>). To my surprise all record of the bug has disappeared. So I used a <a href="http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=63804455-f93b-48bc-8c92-d5b072479bc9">direct link</a> to the bug, and I got this:</p>
<p><span style="font-size:85%;"><span style="font-family:courier new;">Page is hidden</span></p>
<p><span style="font-family:courier new;">This feedback is not being displayed because it may identify a possible security vulnerability.</span></p>
<p><span style="font-family:courier new;">Please re-submit this feedback, and all other security-related issues, at http://www.microsoft.com/technet/security/bulletin/alertus.aspx. Please refer to our FAQ for more details.</span></span></p>
<p>So apparently the bug is a potential security vulnerability. Actually that makes sense, because it brings the process down. Could easily be due to a buffer overrun or similar issue that could be exploited by a bad guy.</p>
<p>The strange thing is that I&#8217;ve heard nothing from MS on this &#8211; not even an automated mail saying that my bug was closed. And the message on the web page isn&#8217;t too encouraging. Do they really expect me to report the problem <span style="font-weight: bold;">again</span> via a different web page, after they&#8217;ve apparently wiped my original bug report?</p>
<p>Perhaps I should just forget about doing the right thing and <a href="http://www.zerodayinitiative.com/benefits.html">cash in</a>?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=5</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET 2.0 ListView VirtualMode</title>
		<link>http://www.andymcm.com/?p=4</link>
		<comments>http://www.andymcm.com/?p=4#comments</comments>
		<pubDate>Sun, 06 Nov 2005 21:29:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=4</guid>
		<description><![CDATA[The Winforms ListView control has a great new feature in .NET 2.0 &#8211; virtual mode. Some time ago I wrote a text file viewer (for log files) using .NET 1.1, and I had to hack together my own virtual mode &#8211; it worked ok but had a lot of bugs that I never got around [...]]]></description>
			<content:encoded><![CDATA[<p>The Winforms ListView control has a great new feature in .NET 2.0 &#8211; virtual mode. Some time ago I wrote a text file viewer (for log files) using .NET 1.1, and I had to hack together my own virtual mode &#8211; it worked ok but had a lot of bugs that I never got around to fixing. So I was very happy to be able to ditch all that dodgy code by porting to .NET 2.0.</p>
<p>The only problem is that there are a few bugs. I&#8217;ve reported a particularly <a href="http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=63804455-f93b-48bc-8c92-d5b072479bc9">nasty one</a> via the MS feedback center. If you&#8217;re using virtual mode and getting apparently random crashes, check that you haven&#8217;t got any list items containing exactly 260 characters.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=4</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mirroring Sourcesafe check-outs with Robocopy</title>
		<link>http://www.andymcm.com/?p=3</link>
		<comments>http://www.andymcm.com/?p=3#comments</comments>
		<pubDate>Mon, 03 Oct 2005 18:49:00 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.andymcm.com/wordpress/?p=3</guid>
		<description><![CDATA[I wanted a way of backing up my source code edits in near-real-time to a network share. There are plenty of file-sync tools around, but I decided to use Microsoft&#8217;s free Robocopy utility. Here&#8217;s the command-line I&#8217;m using: robocopy c:\src u:\src_mirror /S /MON:1 /XA:R /XD bin obj debug release build /XF *.ncb *.scc *.user *.webinfo [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted a way of backing up my source code edits in near-real-time to a network share. There are plenty of file-sync tools around, but I decided to use Microsoft&#8217;s free Robocopy utility.</p>
<p>Here&#8217;s the command-line I&#8217;m using:</p>
<p><span style="font-family:courier new;">robocopy c:\src u:\src_mirror /S /MON:1 /XA:R /XD bin obj debug release build /XF *.ncb *.scc *.user *.webinfo *.suo /R:5 /W:1</span></p>
<p>The interesting switches are /MON:1, which tells Robocopy to check my source directory for changes once a minute, and /XA:R, which tells it to ignore readonly files. Because of the /XA:R, files checked into Sourcesafe are ignored. Conversely, any files I check-out become read/write and automatically get mirrored.</p>
<p>Obviously there are some non-readonly files and directories I don&#8217;t want to mirror, hence the /XF and /XD switches.</p>
<p>Been using this for a few days, and so far it seems to work great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andymcm.com/?feed=rss2&amp;p=3</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
