<?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>sharpreflections.net &#187; Programming</title>
	<atom:link href="http://blog.sharpreflections.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sharpreflections.net</link>
	<description>Thoughts about .Net, programming and other more important stuffs.</description>
	<lastBuildDate>Fri, 20 Nov 2009 00:24:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple parallel programming</title>
		<link>http://blog.sharpreflections.net/2009/05/18/simple-parallel-programming/</link>
		<comments>http://blog.sharpreflections.net/2009/05/18/simple-parallel-programming/#comments</comments>
		<pubDate>Mon, 18 May 2009 22:59:40 +0000</pubDate>
		<dc:creator>matteosp</dc:creator>
				<category><![CDATA[.Net Programming]]></category>
		<category><![CDATA[ParallelProgramming]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[.Net;Threading;ParallelProgramming]]></category>

		<guid isPermaLink="false">http://blog.sharpreflections.net/?p=105</guid>
		<description><![CDATA[While waiting for Parallel Extensions (wikipedia), that will be shipped with next .Net Framework release, I designed a couple of class that allow to quickly (and easily!) write code that make use of multi-threading and, therefore, speed-up many tasks. What I show here is a simple class able to collect pieces of code (in form [...]]]></description>
			<content:encoded><![CDATA[<p>While waiting for <a title="Parallel Programming with .NET" href="http://blogs.msdn.com/pfxteam" target="_blank">Parallel Extensions</a> (<a title="Parallel Extensions" href="http://en.wikipedia.org/wiki/Parallel_FX_Library" target="_blank">wikipedia</a>), that will be shipped with next .Net Framework release, I designed a couple of class that allow to quickly (and easily!) write code that make use of multi-threading and, therefore, speed-up many tasks. What I show here is a simple class able to collect pieces of code (in form of delegate) to be executed concurrently, and parallelize and synchronize the execution. The main goals I kept in mind while realizing the solution were:</p>
<ul>
<li>Ease of use, minimal requirement in terms of multi-threading skills</li>
<li>Allowing single-threaded code to be ported to multi-threading with few changes</li>
<li>Code readability</li>
<li>Support for exception management</li>
</ul>
<p>Here what I came up with:</p>
<p>The API exposes one class, <span class="csharpcode">ParallelExecutor</span>, and one interface, <span class="csharpcode">IExecutionResult</span> (plus a generic specialization of the interface and the necessary implementations). Before seeing the internals of the solution, I want to show <strong>the ease of use of the API</strong>.<br />
Let&#8217;s start with this example:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">void</span> SerialExecution()</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    Uri uri; <span class="kwrd">int</span> <span class="kwrd">value</span>;</pre>
<pre><span class="lnum">   4:  </span></pre>
<pre class="alt"><span class="lnum">   5:  </span>    CallWebService(uri);</pre>
<pre><span class="lnum">   6:  </span>    PerformComputation(<span class="kwrd">value</span>);</pre>
<pre class="alt"><span class="lnum">   7:  </span>}</pre>
</div>
<p><span class="csharpcode">CallWebService()</span> and <span class="csharpcode">PerformComputation()</span> represent the most classic example of methods that can (and should!) run in parallel as they perform respectively I/O and CPU computation. But usually programmers haven&#8217;t enough confidence with multi-threading execution and synchronization to do things in the right way. In any case they shouldn&#8217;t care about this stuff, they should focus on coding logic, and not on coding about how the logic get&#8217;s executed. Let&#8217;s see how <span class="csharpcode">ParallelExecutor</span> comes in help:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">void</span> ParallelExecution</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>  Uri uri; <span class="kwrd">int</span> <span class="kwrd">value</span>;</pre>
<pre><span class="lnum">   4:  </span></pre>
<pre class="alt"><span class="lnum">   5:  </span>  ParallelExecutor executor = <span class="kwrd">new</span> ParallelExecutor();</pre>
<pre><span class="lnum">   6:  </span></pre>
<pre class="alt"><span class="lnum">   7:  </span>  executor.Add(CallWebService, uri);</pre>
<pre><span class="lnum">   8:  </span>  executor.Add(PerformComputation, <span class="kwrd">value</span>);</pre>
<pre class="alt"><span class="lnum">   9:  </span></pre>
<pre><span class="lnum">  10:  </span>  executor.WaitAll();</pre>
<pre class="alt"><span class="lnum">  11:  </span>}</pre>
</div>
<p>Not too bad, right? With a couple of lines I got the two methods run concurrently. This is pretty simple: the Add() method takes as arguments the method to invoke and the arguments of that method. The WaitAll() causes the calling thread to wait for all the methods passed to Add() to complete. Take a (partial) look to the <span class="csharpcode">ParallelExecutor</span> definition:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> ParallelExecutor</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>  <span class="kwrd">public</span> IExecutionInfo&lt;<span class="kwrd">object</span>&gt; Add(Action workItem)</pre>
<pre><span class="lnum">   4:  </span>  <span class="kwrd">public</span> IExecutionInfo&lt;<span class="kwrd">object</span>&gt; Add&lt;T&gt;(Action&lt;T&gt; workItem, T arg)</pre>
<pre class="alt"><span class="lnum">   5:  </span>  <span class="rem">//overloads for other Action versions </span></pre>
<pre><span class="lnum">   6:  </span></pre>
<pre class="alt"><span class="lnum">   7:  </span>  <span class="kwrd">public</span> IExecutionInfo&lt;TResult&gt; Add&lt;TResult&gt;(Func&lt;TResult&gt; workItem)</pre>
<pre><span class="lnum">   8:  </span>  <span class="kwrd">public</span> IExecutionInfo&lt;TResult&gt; Add&lt;T, TResult&gt;(Func&lt;T, TResult&gt; workItem, T arg)</pre>
<pre class="alt"><span class="lnum">   9:  </span>  <span class="rem">//overloads for other Func versions </span></pre>
<pre><span class="lnum">  10:  </span></pre>
<pre class="alt"><span class="lnum">  11:  </span>  <span class="kwrd">public</span> <span class="kwrd">bool</span> WaitAll():</pre>
<pre><span class="lnum">  12:  </span>}</pre>
</div>
<p>As you can see, method to be parallelized are passed to Add() in form of variations of Action and Func generics delegate (there are also overloads that take <span class="csharpcode">MethodInfo</span> and the standard <span class="csharpcode">Delegate</span>). But what about the Add() return type, <span class="csharpcode">IExecutionResult</span>? Let&#8217;s see the interface declaration:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">interface</span> IExecutionInfo</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>  <span class="kwrd">object</span>[] Args { get; }</pre>
<pre><span class="lnum">   4:  </span></pre>
<pre class="alt"><span class="lnum">   5:  </span>  Exception Exception { get; }</pre>
<pre><span class="lnum">   6:  </span></pre>
<pre class="alt"><span class="lnum">   7:  </span>  <span class="kwrd">object</span> Result { get; }</pre>
<pre><span class="lnum">   8:  </span>}</pre>
<pre class="alt"><span class="lnum">   9:  </span></pre>
<pre><span class="lnum">  10:  </span><span class="kwrd">public</span> <span class="kwrd">interface</span> IExecutionInfo&lt;TResult&gt; : IExecutionInfo</pre>
<pre class="alt"><span class="lnum">  11:  </span>{</pre>
<pre><span class="lnum">  12:  </span>  TResult Result { get; }</pre>
<pre class="alt"><span class="lnum">  13:  </span>}</pre>
</div>
<p>At every invocation of the Add() method an instance of a class implementing <span class="csharpcode">IExecutionResult</span> is created, and its Args property is filled with method invocation arguments. After the invocation completes, the Result and Exception property are filled too (in both case, if any). <strong>And the magic of generics allow the code to be type safe</strong>. So, returning to the example, if CallWebService() has a return value and if you want to check for exceptions, you can write:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">void</span> ParallelExecution</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>  Uri uri; <span class="kwrd">int</span> <span class="kwrd">value</span>;</pre>
<pre><span class="lnum">   4:  </span></pre>
<pre class="alt"><span class="lnum">   5:  </span>  ParallelExecutor executor = <span class="kwrd">new</span> ParallelExecutor();</pre>
<pre><span class="lnum">   6:  </span></pre>
<pre class="alt"><span class="lnum">   7:  </span>  IExecutionInfo&lt;<span class="kwrd">int</span>&gt; execution = executor.Add&lt;Uri, <span class="kwrd">int</span>&gt;(CallWebService, uri);</pre>
<pre><span class="lnum">   8:  </span>  executor.Add(PerformComputation, <span class="kwrd">value</span>);</pre>
<pre class="alt"><span class="lnum">   9:  </span></pre>
<pre><span class="lnum">  10:  </span>  executor.WaitAll();</pre>
<pre class="alt"><span class="lnum">  11:  </span></pre>
<pre><span class="lnum">  12:  </span>  <span class="kwrd">if</span> (execution.Exception != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  13:  </span>      ManageException(execution.Exception);</pre>
<pre><span class="lnum">  14:  </span>  <span class="kwrd">else</span></pre>
<pre class="alt"><span class="lnum">  15:  </span>      Console.WriteLine(<span class="str">"Result: {0}"</span>, execution.Result);</pre>
<pre><span class="lnum">  16:  </span>}</pre>
</div>
<p>Another couple of properties exposed by <span class="csharpcode">ParallelExecutor</span> allow you to control when the execution starts (immediately after Add() or when WaitAll() gets called) and the aggressiveness of the threading policy (one new Thread for every method passed to Add() or threads got from ThreadPool).</p>
<p>Obviously this is a very simple implementation, far away from what Parallel Extensions will be. But its simplicity may suite well some implementation with no heavy requirements.</p>
<p><a title="Simple parallel programming (MSDN Code Gallery)" href="http://code.msdn.microsoft.com/SimpleParallelExt" target="_blank">Source Code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharpreflections.net/2009/05/18/simple-parallel-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How I got Started in Software Development</title>
		<link>http://blog.sharpreflections.net/2008/07/30/how-i-got-started-in-software-development/</link>
		<comments>http://blog.sharpreflections.net/2008/07/30/how-i-got-started-in-software-development/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 22:31:41 +0000</pubDate>
		<dc:creator>matteosp</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sharpreflections.wordpress.com/?p=105</guid>
		<description><![CDATA[I had the great pleasure of having been tagged by Adrian. So&#8230; it&#8217;s my turn:

How old were you when you started programming?

In 1994, when I was 16 and I was at the high school. But I must say also that at the age of about 10 year a &#8220;played&#8221;  with BASIC on a Laser [...]]]></description>
			<content:encoded><![CDATA[<p>I had the great pleasure of having been tagged by <a title="Web Log di Adrian Florea" href="http://blogs.ugidotnet.org/adrian" target="_blank">Adrian</a>. So&#8230; it&#8217;s my turn:</p>
<p>
<h3>How old were you when you started programming?</h3>
</p>
<p>In 1994, when I was 16 and I was at the high school. But I must say also that at the age of about 10 year a &#8220;played&#8221;  with BASIC on a <a title="Laser 500" href="http://old-computers.com/museum/computer.asp?c=449" target="_blank">Laser 500</a>, something similar to a Commodre 64.</p>
<p>
<h3>How did you get started in programming?</h3>
</p>
<p>&#8230; in those days I learned <a title="Turbo Pascal" href="http://en.wikipedia.org/wiki/Turbo_Pascal" target="_blank">Turbo Pascal</a>, and I used it to solve not too complex math and physics problems.</p>
<p>
<h3>What was your first language?</h3>
</p>
<p>As I said, the very first language was Pascal. But, as a professional, I consider my first language to be Visual Basic 6. It is for sure the language that made me falling in love with programming.</p>
<p>
<h3>What was the first real program you wrote?</h3>
</p>
<p>At the end of a one year programming training I build a RSE &#8211; really simple ERP <img src='http://blog.sharpreflections.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  &#8211; for demo purposes. A WinForm application over an MS Access database. Presentation Layer e Business Logic were mixed up in the Visual Basic 6 forms, but I think here I wrote my first Data Layer.</p>
<p>
<h3>What languages have you used since you started programming?</h3>
</p>
<p>In order Basic, Turno Pascal, C++, Visual Basic 6, Java, PHP, ASP, JavaScript, VB Script, C#, Visual Basic .Net, Python.</p>
<p>
<h3>What was your first professional programming gig?</h3>
</p>
<p>In <a title="we make it." href="http://www.brainforce.it" target="_blank">Brain Force</a>, the company I&#8217;m still working for. 6 years ago.</p>
<p>
<h3>If you knew then what you know now, would you have started programming?</h3>
</p>
<p>I love programming: it consume a lot of resources but gives back a lot of satisfaction. But I would have choosen something else.</p>
<p>
<h3>If there is one thing you learned along the way that you would tell new developers, what would it be?</h3>
</p>
<p>Start from the theory and with the doc. Ever. Ever!! And pay attention to who wrote what you read.</p>
<p>
<h3>What&#8217;s the most fun you&#8217;ve ever had &#8230; programming?</h3>
</p>
<p>An ORM. I&#8217;m really proud of it.</p>
<p>
<h3>Now, let’s tag someone else&#8230;</h3>
</p>
<ul>
<li><a title="Pietro Toniolo" href="http://ptoniolo.wordpress.com/" target="_blank">Pietro</a></li>
<li><a title="Il blog di Marco Fanfoni" href="http://blogs.ugidotnet.org/marcofan" target="_blank">Marco</a></li>
<li><a title="Il blog di Doc Ge" href="http://blogs.ugidotnet.org/alessage" target="_blank">Ale</a></li>
<li><a title="Lanny's blog" href="http://blogs.ugidotnet.org/lanny" target="_blank">Lanny</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharpreflections.net/2008/07/30/how-i-got-started-in-software-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The final truth about OOP</title>
		<link>http://blog.sharpreflections.net/2008/04/14/the-final-truth-about-object-oriented-programming/</link>
		<comments>http://blog.sharpreflections.net/2008/04/14/the-final-truth-about-object-oriented-programming/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 19:22:31 +0000</pubDate>
		<dc:creator>matteosp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[Software sucks]]></category>

		<guid isPermaLink="false">http://sharpreflections.wordpress.com/?p=104</guid>
		<description><![CDATA[My guess is that object-oriented programming will be in the 1980s what structured programming will be in 1970s. Everyone will be in favor of it. Every manufacturer will promote his products as supporting it. Every manager will pay lip service to it. Every programmer will practice it (differently). And no one will know just what [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>My guess is that object-oriented programming will be in the 1980s what structured programming will be in 1970s. Everyone will be in favor of it. Every manufacturer will promote his products as supporting it. Every manager will pay lip service to it. Every programmer will practice it (differently). And no one will know just what it is.</p></blockquote>
<p><strong>T. Rentsch</strong></p>
<p>The more I read these words, the more I&#8217;m convinced this is one of the more accurate prediction I&#8217;ve heard about in programming. I feel it so&#8230; real.</p>
<p>via Booch, Grady. <em>Object-Oriented Analysis and Design with Applications</em>, Second Edition. Boston: Addison-Wesley, 1993. <a title="on Amazon UK" href="http://www.amazon.co.uk/Object-Oriented-Analysis-Design-Applications/dp/0805353402/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1208200781&amp;sr=1-2" target="_blank">Link</a>.<br />
via West, David. <em>Object Thinking</em>. Redmon: Microsft Press, 2003. <a title="Object Thinking on Amazon UK" href="http://www.amazon.co.uk/Object-Thinking-DV-Microsoft-Professional-West/dp/0735619654/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1208200710&amp;sr=8-1" target="_blank">Link</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sharpreflections.net/2008/04/14/the-final-truth-about-object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
