<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coding Powers</title>
	<atom:link href="http://codingpowers.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingpowers.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sun, 15 Jan 2012 09:34:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codingpowers.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Coding Powers</title>
		<link>http://codingpowers.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codingpowers.wordpress.com/osd.xml" title="Coding Powers" />
	<atom:link rel='hub' href='http://codingpowers.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Building a FIX engine in .NET</title>
		<link>http://codingpowers.wordpress.com/2012/01/15/building-a-fix-engine-in-net/</link>
		<comments>http://codingpowers.wordpress.com/2012/01/15/building-a-fix-engine-in-net/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 09:20:38 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[1. Financial]]></category>
		<category><![CDATA[FIX Engine]]></category>
		<category><![CDATA[Problems and Solutions]]></category>
		<category><![CDATA[Traders Tools]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Automated trading]]></category>
		<category><![CDATA[Broker]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[FIX]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=489</guid>
		<description><![CDATA[What is FIX The Financial Information Exchange Protocol is defined on the homepage of the protocol standards web site as “The Financial Information eXchange (FIX) Protocol is a messaging standard developed specifically for the real-time electronic exchange of securities transactions.” However, I think this definition is very limited. The protocol does not just define a messaging standard [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=489&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>What is FIX</strong></p>
<p>The <a href="http://fixprotocol.org/">Financial Information Exchange Protocol</a> is defined on the homepage of the protocol standards web site as “The <strong>Financial Information eXchange (FIX) Protocol</strong> is a messaging standard developed specifically for the real-time electronic exchange of securities transactions.”</p>
<p>However, I think this definition is very limited. The protocol does not just define a messaging standard but also how the two systems communicate including session management, encryption and login details.</p>
<p>In my own words “FIX is a protocol which defines the messaging standard and how those messages should be passed back and forwards, encryption and communication failure recovery between two or more financial systems specifically for the real-time exchange of a number of different financial instruments.”</p>
<p><strong>Why build a FIX engine in .NET</strong></p>
<p>Several attempts have been made to build open source .NET implementations of a FIX engine. However, these engines have limitations which include:</p>
<ul>
<li>Written in old versions of .NET so they don’t take into account generics or the new thread-safe libraries</li>
<li>Are just a wrapper around a different technology such as Java or C++</li>
<li>Are not open source and therefore just supplied as binaries</li>
<li>Inefficient or un-configurable logging and auditing systems</li>
</ul>
<p><strong>How will the engine be used?</strong></p>
<p>The FIX engine will be used in a part manual and part automated trading system to manipulate orders on one or many remote broker systems.</p>
<p><strong>Which implementation of FIX will be used?</strong></p>
<p>As different broker systems use different versions of FIX, the .NET FIX engine should be able to communicate with any version from 4.0 to the latest version.</p>
<p><strong>Important features of the engine</strong></p>
<p>The engine should be fast, stable and provide an easy to use API. All messages should be logged for replay, order history and audit purposes. It should include all of the functionality to enable session creation, replay of missed messages, encryption and checksum validation. It should be easy to debug, maintain and have 100% test coverage.</p>
<p><strong>How to build for an entire protocol</strong></p>
<p>Covering an entire protocol is a large and daunting task. So as with any large and difficult task it is good to break it down into smaller pieces. The first instance will be to create a machine readable specification of the protocol so that we can auto-generate the large number of message types needed to cover the different versions of the protocol. We may be able to get this or some of this from the FIXimate part of the protocol website or use the word documentation which forms the <a href="http://fixprotocol.org/specifications/">protocol definition</a> for FIX.</p>
<p>As well as coding the messages we will also need to provide an engine to create an open session for communication including logon and encryption and the ability to send and replay messages should they not be received by the target host. We will write this engine from scratch using the recommended approach defined on the FIX web site through their training sessions.</p>
<p>This should give us a good start to providing an engine which we can refine in a test harness / sandbox on a broker account.</p>
<p><strong>Testing the solution</strong></p>
<p>We will use TDD for testing unit functions through unit tests and provide full integration testing against a broker’s sandbox system.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/489/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/489/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/489/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=489&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2012/01/15/building-a-fix-engine-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Producer – Consumer Queue in .NET 4 part 2</title>
		<link>http://codingpowers.wordpress.com/2012/01/08/simple-producer-consumer-queue-in-net-4-part-2/</link>
		<comments>http://codingpowers.wordpress.com/2012/01/08/simple-producer-consumer-queue-in-net-4-part-2/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 10:40:00 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[2. Application Development]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[.NET4]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[producer consumer]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=480</guid>
		<description><![CDATA[Please first read Part one of the simple producer – consumer queue in .net4. In part 1 we defined a standard producer – consumer queue, however, there was nothing that particularly made this stand out from a queue written in a previous version of .Net. In this post, I am going to upgrade the queue [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=480&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Please first read <a href="http://codingpowers.wordpress.com/2011/12/29/simple-producer-consumer-queue-in-net-4/">Part one of the simple producer – consumer queue in .net4</a>.</p>
<p>In part 1 we defined a standard producer – consumer queue, however, there was nothing that particularly made this stand out from a queue written in a previous version of .Net. In this post, I am going to upgrade the queue to allow for the following:</p>
<ul>
<li>Take advantage of the thread safe collections in .Net 4</li>
<li>Allow for the producer to know when a task has been completed</li>
<li>Cancel un-started tasks</li>
<li>Handle exceptions thrown by the tasks</li>
</ul>
<p>The code for this example has mostly come from the book <a href="http://codingpowers.wordpress.com/books/">C#4 in a nutshell</a> with some modifications from me.</p>
<p>First we will modify our queue class. I have renamed the class to PCQueue4 to differentiate them.</p>
<p>The first thing to notice about this Class is that instead of queuing actions directly on a queue, we are encapsulating these actions in a workitem wrapper.  I have made this wrapper a Struct instead of a class to avoid have this set of objects on the heap to avoid excessive garbage collections.</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">

///
<summary>
        /// Class to encapsulate an item on the queue
        /// </summary>

        struct WorkItem
        {
            public readonly TaskCompletionSource TaskSource;
            public readonly Action Action;
            public readonly CancellationToken? CancelToken;

            public WorkItem(TaskCompletionSource taskSource, Action action, CancellationToken? cancelToken)
            {
                TaskSource = taskSource;
                Action = action;
                CancelToken = cancelToken;
            }
        }

</code></pre>
<p>The wrapper workitem has three properties. They are the action enqueued by the producer, a CancellationToken and a reference to the actual Task itself through the TaskSource&lt;T&gt;  class.</p>
<p>We will use these other properties to achieve our goal of cancellation, exception handling and for progress reports.</p>
<p>The next change is to change the collection that we are using from the Thread Unsafe Queue, to the new .Net 4 Threadsafe collection BlockingCollection&lt;Action&gt;.</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;"> 

BlockingCollection&lt;WorkItem&gt;
 _itemQ = new BlockingCollection&lt;WorkItem&gt;();   

</code></pre>
<p>The next change is to the construction of our worker threads. In our previous example we created the thread directly using the Thread class, however, this time we will use the new Task.Factory.StartNew() method to create our thread as a Task object on the thread pool. Having these threads in the pool will also improve performance.</p>
<p>For our Dispose() method, we will take advantage of the method CompleteAdding() which is called on the BlockingCollection. This tells the collection that no more items will be added (trying to add will throw an ObjectExposedException) and it also stops any thread waiting on the collection if it is empty.</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;"> 

public void Dispose(){_itemQ.CompleteAdding();}  

</code></pre>
<p>To enqueue a Task we have an overloaded method EnqueueTask() which allows the producer to add an Action to the queue. The overloaded method is to allow for cancellation through a cancellation token.</p>
<p>Finally the last change is the Consume() method. This is where the really cool changes are made and the reason why this collection is so compelling to use.</p>
<p>The first thing you notice about this method is that we are not calling Thread.Wait() and we are not entering into an infinite loop with while(true)</p>
<p>The foreach loop which calls _itemQ.GetConsumingEnumerable() is the key to how this queue stays alive. This method is a blocking method. Build into the collection is a Wait() when the collection is empty. It is practically made for consumer / producer queues.</p>
<p>We then check the cancellation token to see if this task has been cancelled. If it has been we set the transition the underlying task into the Cancelled state.</p>
<p>This will throw an operationCancelledException which will be rolled up in the AggregateException which we will handle later.</p>
<p>If the Task has not been cancelled we can process the Task (Action) and mark it as completed using the SetResult method on TaskCompletionSource object which again transitions the Task into a new state. (RanToCompletion)</p>
<p>The full source is here:</p>
<p>PcQueue4:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">

using System;

using System.Collections.Concurrent;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

namespace StreamingProducer

{

public class PcQueue4 : IDisposable

{

/// &lt;summary&gt;

/// Class to encapsulate an item on the queue

/// &lt;/summary&gt;

struct WorkItem

{

public readonly TaskCompletionSource&lt;object&gt; TaskSource;

public readonly Action Action;

public readonly CancellationToken? CancelToken;

public WorkItem(TaskCompletionSource&lt;object&gt; taskSource, Action action, CancellationToken? cancelToken)

{

TaskSource = taskSource;

Action = action;

CancelToken = cancelToken;

}

}

BlockingCollection&lt;WorkItem&gt; _itemQ = new BlockingCollection&lt;WorkItem&gt;();

public PcQueue4(int workerCount)

{

for(int i = 0;i&lt;workerCount;i++)

{

Task.Factory.StartNew(Consume);

}

}

public void Dispose(){_itemQ.CompleteAdding();}

public Task EnqueueTask(Action action)

{

return EnqueueTask(action, null);

}

public Task EnqueueTask(Action action,CancellationToken? cancelToken)

{

var tcs = new TaskCompletionSource&lt;object&gt;();

_itemQ.Add(new WorkItem(tcs,action,cancelToken));

return tcs.Task;

}

void Consume()

{

foreach (var workItem in _itemQ.GetConsumingEnumerable())

{

if(workItem.CancelToken.HasValue &amp;&amp; workItem.CancelToken.Value.IsCancellationRequested)

{

workItem.TaskSource.SetCanceled();

}

else

{

try

{

workItem.Action();

workItem.TaskSource.SetResult(null);//indicate completion

}

catch (Exception ex)

{

workItem.TaskSource.SetException(ex);

}

}

}

}

}

}

</code></pre>
<p>And the way to call this from the Producer:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">

Task task = EventQueue.EnqueueTask(() =&gt; _service.UpdateListItem(update));

			if(task.Exception != null)
			{
				AggregateException ex = task.Exception.Flatten();
				foreach (var innerException in ex.InnerExceptions) { }//do logging
				_tmr.Stop();
			}

</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=480&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2012/01/08/simple-producer-consumer-queue-in-net-4-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit testing for managed memory leakage</title>
		<link>http://codingpowers.wordpress.com/2011/12/31/unit-testing-for-managed-memory-leakage/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/31/unit-testing-for-managed-memory-leakage/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 23:05:37 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Problems and Solutions]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[GC - Memory Management]]></category>
		<category><![CDATA[jet brains]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[weak references]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=470</guid>
		<description><![CDATA[It might seem a strange thing to do. Firstly can there be managed memory leakage? And secondly how can you unit test for this? I’ll put this into context and then all will become clear. I came across this interesting problem recently when playing around with a set of streaming real time data and attaching [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=470&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It might seem a strange thing to do. Firstly can there be managed memory leakage? And secondly how can you unit test for this?</p>
<p>I’ll put this into context and then all will become clear. I came across this interesting problem recently when playing around with a set of streaming real time data and attaching an observable collection to it so that I could view the changes in a WPF application.</p>
<p>The application was holding on to objects which should have been disposed after they went out of scope. These objects did not have their memory de-allocated by the Garbage Collector but nothing seemed to be referencing them. (i.e. they had no GC Root object or referee that I could see)</p>
<p>It turned out that the objects had subscribed to an event on an object which was still alive. (in scope) and this was not being cleaned up when the objects went out of scope. I wanted a way to overcome this problem and also to test for this kind of problem in the future as I am likely to forget to remove subscriptions again.</p>
<p>I did a bit of research and came up with the simple solution which is to implement IDisposable on each object and remove the event subscription on each call to Dispose().</p>
<p>The consumer of these objects can do something like this</p>
<p>Array.ForEach(obj, o=&gt; o.Dispose());</p>
<p>And the Dispose() method would have something like:</p>
<p>_memoryLeak.Click -= ItemClicked;</p>
<p>This sorts me out this time, but what about testing for this condition across my unit tests?</p>
<p>I did some more Googling and thanks to Joseph Albahari I discovered the call GC.GetTotalMemory(true);</p>
<p>This static call to the GC class with the parameter true, performs a full Garbage collection and then determines all of the memory allocated to the program.</p>
<p>However, it is virtually impossible to test your code as is, using this method. The memory usage will change because of the test itself. I found that I could get the result I wanted but the tests have to be very granular indeed and not introduce any additional overhead or variables in otherwise it won’t work.</p>
<p>Debugging memory management is more about tuning and finding the big problems. The smaller the problem the more time spent on it and each fine tune has a cost-benefit analysis involved.</p>
<p>Here is my example:</p>
<p>The classes to test which have a memory leak:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">using System; </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;">namespace MemoryLeamSimilator </code><code style="display:block;margin:0 0 0 5px;">{ </code><code style="display:block;margin:0 0 0 5px;"> public class MemoryLeak </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> public event EventHandler Click; </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public class MemoryItemsToBeDisposed :IDisposable </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> private MemoryLeak _memoryLeak; </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public MemoryItemsToBeDisposed(MemoryLeak memoryLeak) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> _memoryLeak = memoryLeak; </code><code style="display:block;margin:0 0 0 5px;"> _memoryLeak.Click +=ItemClicked; </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> void ItemClicked(object sender, EventArgs e) </code><code style="display:block;margin:0 0 0 5px;"> { //do something } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public void Dispose() </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> _memoryLeak.Click -= ItemClicked; </code><code style="display:block;margin:0 0 0 5px;"> }</code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;">} </code><code style="display:block;margin:0 0 0 5px;"> </code></pre>
<p>The unit test for memory leak detection:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">using System; </code><code style="display:block;margin:0 0 0 5px;">using NUnit.Framework; </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;">namespace MemoryLeamSimilator </code><code style="display:block;margin:0 0 0 5px;">{ </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;">[TestFixture] </code><code style="display:block;margin:0 0 0 5px;">public class MemoryLeakTests </code><code style="display:block;margin:0 0 0 5px;">{ </code><code style="display:block;margin:0 0 0 5px;"> private static MemoryLeak _memoryLeak = new MemoryLeak(); </code><code style="display:block;margin:0 0 0 5px;"> private long _memoryUsedBefore = 0; </code><code style="display:block;margin:0 0 0 5px;"> private long _memoryUsedAfter = 0; </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> [Test, Description("Test the memory is de-allocated")] </code><code style="display:block;margin:0 0 0 5px;"> public void CreateItemsMemoryTest() </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> _memoryUsedBefore = GC.GetTotalMemory(true); </code><code style="display:block;margin:0 0 0 5px;"> var ml = new MemoryItemsToBeDisposed(_memoryLeak); </code><code style="display:block;margin:0 0 0 5px;"> ml.Dispose(); </code><code style="display:block;margin:0 0 0 5px;"> ml = null; //need this </code><code style="display:block;margin:0 0 0 5px;"> _memoryUsedAfter = GC.GetTotalMemory(true); </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> Assert.AreEqual(_memoryUsedBefore, _memoryUsedAfter); </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"><span style="background-color:#ffff99;font-size:12px;">}</span> </code><code style="display:block;margin:0 0 0 5px;"><span style="background-color:#ffff99;font-size:12px;">}</span> </code><code style="display:block;margin:0 0 0 5px;"> </code></pre>
<p>You can see this test fail by commenting out the .Dispose() line.</p>
<p>Of course, this problem will still exist if the developer who uses the library forgets to call dispose. You can address this with <a href="http://codingpowers.wordpress.com/2010/04/16/dispose-pattern/">the Dispose pattern</a>. (Just substitute the release of the COM object with the code to release the event handlers in this example)</p>
<p><strong>Note:</strong></p>
<p>The test will also fail is you do not set the reference of the item to null.</p>
<p><strong>Profiling:</strong></p>
<p>To profile the application you can use <a href="http://www.jetbrains.com/profiler/index.html">Jet Brains’ Dot Trace</a>, MS CLR Profiler or Red Ant’s Memory Profiler.</p>
<p>As this solution is very tricky to test and will only work if there is almost no set up required in your unit test to call the method under test, I need another way of testing this. The next approach is to use Weak References.</p>
<p>Weak References are a way of referencing a heap object in code but have the GC ignore this link when analysing an object graph. By setting up Weak Reference objects and then letting the underlying object go out of scope, doing a GC.Collect() and testing the WeakReference.Target for null, we can see if the object still exists on the Heap.</p>
<p>The unit test for this is here:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;">[Test, Description("Test the memory is de-allocated")] </code><code style="display:block;margin:0 0 0 5px;">public void CreateItemsWeakReferenceTest() </code><code style="display:block;margin:0 0 0 5px;">{ </code><code style="display:block;margin:0 0 0 5px;"> var ml = new MemoryItemsToBeDisposed(_memoryLeak); </code><code style="display:block;margin:0 0 0 5px;"> var weakReference = new WeakReference(ml); </code><code style="display:block;margin:0 0 0 5px;"> ml.Dispose(); </code><code style="display:block;margin:0 0 0 5px;"> ml = null; </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> GC.Collect();</code><code style="display:block;margin:0 0 0 5px;"> Assert.IsNull(weakReference.Target); </code><code style="display:block;margin:0 0 0 5px;">}</code><code style="display:block;margin:0 0 0 5px;"> </code></pre>
<p>Again, if you comment out the Dispose() line you will see that the GC did not clean up the object and the test will fail.</p>
<p><strong>Conclusion</strong></p>
<p>I think for most cases for specific memory de-allocation tests I will use a WeakReference object, but it is good to have the bytes solution there in the testing armament.</p>
<p>Note:</p>
<p>One other solution for this problem as provided in <a href="http://codingpowers.wordpress.com/books/">the C#4 in a nutshell book</a>, is to use WeakReferences instead of event handlers, which will remove this problem altogether.</p>
<p align="right">
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/470/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=470&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/31/unit-testing-for-managed-memory-leakage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Producer – Consumer Queue in .NET 4 part 1</title>
		<link>http://codingpowers.wordpress.com/2011/12/29/simple-producer-consumer-queue-in-net-4/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/29/simple-producer-consumer-queue-in-net-4/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 21:15:32 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[Back to basics - C#]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[producer consumer]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=462</guid>
		<description><![CDATA[Another back to basics post for reference material . I will follow this post with how to improve this basic pattern with .NET 4 objects. In building a real time news aggregator which requires a producer – consumer queue to provide up to the second news feeds for a trading engine, I have updated my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=462&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Another back to basics post for reference material <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . I will follow this post with how to improve this basic pattern with .NET 4 objects.</p>
<p>In building a real time news aggregator which requires a producer – consumer queue to provide up to the second news feeds for a trading engine, I have updated my general pattern for .NET4. So this seemed like a good excuse for a post.</p>
<p>The pattern is used for allowing a multi-threaded execution of a set of tasks which are being produced, usually continuously and the tasks are discrete functional items that do need to interact with each other.</p>
<p>The idea is that the producer of the tasks simply loads the tasks as it produces them to a Queue. The Queue has a number of threaded workers that pull items of the queue and execute them as fast as they can.</p>
<p>The queue can be implemented using the <a href="http://msdn.microsoft.com/en-us/library/7977ey2c.aspx">.NET Queue class</a> which is a first in, first out collection of items which we can load Action or Delegate types to.</p>
<p>The implementation of the Queue for this pattern is as follows:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">public class PCQueue : IDisposable </code><code style="display:block;margin:0 0 0 5px;">{ </code><code style="display:block;margin:0 0 0 5px;"> private Thread[] _workers; </code><code style="display:block;margin:0 0 0 5px;"> Queue&lt;Action&gt; _itemQ = new Queue&lt;Action&gt;(); </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public PCQueue(int workerCount) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> _workers = new Thread[workerCount]; </code><code style="display:block;margin:0 0 0 5px;"> for(int i = 0;i&lt;workerCount;i++) </code><code style="display:block;margin:0 0 0 5px;"> {  </code><code style="display:block;margin:0 0 0 5px;"> (_workers[i] = new Thread(Consume)).Start(); </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> /// &lt;summary&gt; </code><code style="display:block;margin:0 0 0 5px;"> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. </code><code style="display:block;margin:0 0 0 5px;"> /// &lt;/summary&gt;  </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public void Dispose() </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> foreach (var thread in _workers) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> EnqueueItem(null); </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> private void Consume() </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> while(true) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> Action item; </code><code style="display:block;margin:0 0 0 5px;"> lock (_itemQ) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> while (_itemQ.Count == 0) </code><code style="display:block;margin:0 0 0 5px;"> {</code><code style="display:block;margin:0 0 0 5px;"> Monitor.Wait(_itemQ); </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> item = _itemQ.Dequeue(); </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> if(item == null) { return; } item(); </code><code style="display:block;margin:0 0 0 5px;"> }</code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code><code style="display:block;margin:0 0 0 5px;"> public void EnqueueItem(Action Item) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> lock (_itemQ) </code><code style="display:block;margin:0 0 0 5px;"> { </code><code style="display:block;margin:0 0 0 5px;"> _itemQ.Enqueue(Item); </code><code style="display:block;margin:0 0 0 5px;"> Monitor.Pulse(_itemQ); </code><code style="display:block;margin:0 0 0 5px;"> }  </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> } </code><code style="display:block;margin:0 0 0 5px;"> </code></pre>
<p>The PCQueue object is instantiated with the number of worker threads that we want the queue to use. Each worker thread is immediately started with the Consume() method of the queue.</p>
<p>The Consume() method enters an infinite loop which continually tries to ‘consume’ or execute the tasks on the Queue. When the Queue has no more items on it, the thread blocks and waits until it receives a signal (pulse) from the EnqueueItem method. The method also has a way of exiting the loop. When a null item is placed on the Queue, the loop with exit and the method with terminate which will also terminate the worker thread.</p>
<p>The EnqueueItem method, simply add a new item to the Queue and uses Pulse to signal to the next waiting thread to try to execute (dequeue) the next item.</p>
<p>The producer code can go something like this:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;"> EventQueue.EnqueueItem(() =&gt; _service.UpdateListItem(update)); </code></pre>
<p>This line could be called in a timer or when stock data or in my case when a new news article is found.</p>
<p>Watch for the next post on .NET 4 improvements.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/462/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/462/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/462/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=462&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/29/simple-producer-consumer-queue-in-net-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>To thyne ownself be true</title>
		<link>http://codingpowers.wordpress.com/2011/12/18/to-thyne-ownself-be-true/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/18/to-thyne-ownself-be-true/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 16:07:09 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[3. General Comment]]></category>
		<category><![CDATA[Shakespeare]]></category>
		<category><![CDATA[to thyne ownself be true]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=460</guid>
		<description><![CDATA[I had a really nice thing said about me today. That the following is true of me and the way I deal with people. I thought I would write it here as I am very humbled by it. &#8220;This above all: to thine own self be true, And it must follow, as the night the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=460&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a really nice thing said about me today. That the following is true of me and the way I deal with people. I thought I would write it here as I am very humbled by it.</p>
<p>&#8220;This above all: to thine own self be true,<br />
And it must follow, as the night the day,<br />
Thou cans&#8217;t not be false to any man&#8221;.</p>
<p>Which is of course Shakespeare.</p>
<p>Life is good and you know you are on the right track when people say things like that to you.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/460/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=460&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/18/to-thyne-ownself-be-true/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Building a news aggregator module for a trading platform –The architecture</title>
		<link>http://codingpowers.wordpress.com/2011/12/16/building-a-news-aggregator-module-for-a-trading-platform-the-architecture/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/16/building-a-news-aggregator-module-for-a-trading-platform-the-architecture/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 09:10:27 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[1. Financial]]></category>
		<category><![CDATA[2. Application Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Problems and Solutions]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Traders Tools]]></category>
		<category><![CDATA[World View]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[news aggregator]]></category>
		<category><![CDATA[producer consumer]]></category>
		<category><![CDATA[software design]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=455</guid>
		<description><![CDATA[In my recent post introducing the news aggregator I detailed what we are currently building. In this post, I will discuss some very high level architecture and some considerations around how to build the application. My first draft at architecture is to just get the different functional parts of the program identified. Then later we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=455&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my recent <a href="http://codingpowers.wordpress.com/2011/12/12/building-a-news-aggregator-module-for-a-trading-platform-introduction/">post introducing the news aggregator</a> I detailed what we are currently building. In this post, I will discuss some very high level architecture and some considerations around how to build the application.</p>
<p>My first draft at architecture is to just get the different functional parts of the program identified. Then later we can see how we might host these on the appropriate hardware and also how we are going to build in the cost model to enable us to charge for the application.</p>
<p>The functional parts of the application are shown here:</p>
<p><a href="http://codingpowers.files.wordpress.com/2011/12/web-pic.png"><img class="alignnone size-medium wp-image-456" title="Architecture" src="http://codingpowers.files.wordpress.com/2011/12/web-pic.png?w=300&#038;h=196" alt="" width="300" height="196" /></a></p>
<p>I will now discuss each functional block of the application to detail what its function is and how it communicates to the rest of the system. Later we will review the performance when building large sets or complicated relationships and we are retrieving lots of documents.</p>
<p><strong>The internet</strong></p>
<p>The internet will be the source of information for the aggregator. In future we may want other streams of data such as actual market data, but we will cross that bridge later. For now we will make sure we design the application to allow easy plugging in of different data sources.</p>
<p>We have a few options of how we get data from the internet. They are summarised here:</p>
<ul>
<li>Pre-defined URLs which return one page. These might be RSS feeds.</li>
<li>Pre-defined URL starting places for web crawling, such as a company website or a blog.</li>
<li>A search engine. Sending search terms to a search engine as using these results</li>
<li>A subscription service, such as paid news or squawk data</li>
<li>Push data that notifies us of changes to specific records in a datastore</li>
</ul>
<p>There are probably others, but we will look at these sources of data to start with.</p>
<p><strong>Web Client</strong></p>
<p>The web client is the .NET library we will use to send http requests to the internet. We may use other libraries as well, such as our custom RSS reader or web services (WCF) for other sources of data. This container on the diagram represents all these libraries as a source of data entry point to the application.</p>
<p><strong>Producer – Consumer Queue</strong></p>
<p>This is the work horse of the application. It queues requests to the web client library and executes the filter and relationship engine on return of the data. It is a standard threading pattern. I am looking forward to investigating some of the new .NET 4 objects which encapsulate this pattern.</p>
<p><strong>F# Filter and algorithm </strong></p>
<p>The filter system will be written functionally and use the trader defined relationships to determine relevance of data and how to link this data together.</p>
<p><strong>Relationship Manager</strong></p>
<p>The relationship manager is the container and rule set for how the traders world view is put together. This will require GUI elements.</p>
<p><strong>GUI</strong></p>
<p>The GUI will be a WPF application that will display a rich drag and drop interface of the trader’s world view. I am thinking of using reactive extensions to encapsulate the IObserver / IObservable pattern to allow the UI to respond instantly to updates.</p>
<p><strong>Conclusion</strong></p>
<p>I am sure there will be many changes to this, but this is the first iteration of the design. We now need to think how to make this into a payable service.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=455&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/16/building-a-news-aggregator-module-for-a-trading-platform-the-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>

		<media:content url="http://codingpowers.files.wordpress.com/2011/12/web-pic.png?w=300" medium="image">
			<media:title type="html">Architecture</media:title>
		</media:content>
	</item>
		<item>
		<title>Building a news aggregator module for a trading platform -Introduction</title>
		<link>http://codingpowers.wordpress.com/2011/12/12/building-a-news-aggregator-module-for-a-trading-platform-introduction/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/12/building-a-news-aggregator-module-for-a-trading-platform-introduction/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 21:22:10 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[2. Application Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Cool Tools]]></category>
		<category><![CDATA[Problems and Solutions]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Traders Tools]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[development team]]></category>
		<category><![CDATA[iterations]]></category>
		<category><![CDATA[lifecycle]]></category>
		<category><![CDATA[news aggregator]]></category>
		<category><![CDATA[trading]]></category>
		<category><![CDATA[uat]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=449</guid>
		<description><![CDATA[I am working on a project where I going to build a news aggregator which takes certain keywords and searches a predefined set of websites, company statements etc for stories or blog posts that have that keyword in it. The keywords are mostly a combination of company names or stock ticker codes and mood indicators, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=449&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am working on a project where I going to build a news aggregator which takes certain keywords and searches a predefined set of websites, company statements etc for stories or blog posts that have that keyword in it. The keywords are mostly a combination of company names or stock ticker codes and mood indicators, people’s names, hedge fund manager comments etc.</p>
<p>The key differentiator of this project than other news aggregators (of which there are hundreds) is that this allows a configurable set of relationships and correlations to be defined to represent the trader’s world view. The news then streams in and is matched against these relationships to enable a challenge or proof in real time of that view.</p>
<p>The ultimate purpose of this tool is to guide traders in placing trades and making investments by using this up to date and very focused picture of global events.</p>
<p>I thought this would make a great series of blog posts to discuss the architecture, code patterns and methodology used in building this very cool tool. Also when the tool goes to general market, this will be a log of the quality that has gone in to the tool to raise confidence in it as a day to day tool in the traders armoury.</p>
<p>Being a Microsoft guy I will of course use C# to build the application. I’ll use WPF for the GUI and try to build the application using the best practices that I know.</p>
<p>The projects I work on generally have the following life cycle:</p>
<ul>
<li>A requirement or need is identified by one of my clients</li>
<li>Discussion takes place to identify what the need really is, and how this can be best serviced by a technological solution if appropriate</li>
<li>A proof of concept is built which uses best practices as though it was production code.</li>
<li>This POC is then used to build out the application in an iterative way</li>
<li>Each iteration generally lasts from 2 – 4 weeks and has the following parts:
<ul>
<li>Discussion with the client determines the priority of each of the functional items</li>
<li>The highest priority items are scoped and planned into the next iteration</li>
<li>User Acceptance Tests are written by the Client or Tester if we have one</li>
<li>Developers use TDD (tests first) to develop the code</li>
<li>Clients are involved with team meetings answering questions as they arise<br />
(This reduces developer decisions on business items and removes gold plating)</li>
<li>Developers show case the work they have done to client</li>
<li>Feedback then gets either built in, or scheduled for re-prioritization next iteration</li>
<li>UAT  happens</li>
<li>Application changes are rolled to production for the client to start using it</li>
</ul>
</li>
</ul>
<p>They also have the following key elements:</p>
<ul>
<li>All team members having access to ask questions to the client and vice versa</li>
<li>Developers all use the same ubiquitous language as the client</li>
<li>A rich domain model built on top of the common language</li>
<li>Every team member sharing responsibility of make working software on time</li>
<li>Commitment of everyone to deliver working software that is fit for purpose</li>
<li>Dedication to best practices including TDD</li>
<li>Sustainable development</li>
<li>Clear communication of what is being delivered and when</li>
<li>Trust built by the development team from the client who can clearly see the above steps taking place</li>
</ul>
<p>This creates an exciting enthused environment for achieving success. This is the environment we will be building our news aggregator. Next post will cover the general code design.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/449/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=449&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/12/building-a-news-aggregator-module-for-a-trading-platform-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Hedge Funds Legends in their own right</title>
		<link>http://codingpowers.wordpress.com/2011/12/10/hedge-funds-legends-in-their-own-right/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/10/hedge-funds-legends-in-their-own-right/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 20:50:58 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[Hedge Funds]]></category>
		<category><![CDATA[World View]]></category>
		<category><![CDATA[Boomerang]]></category>
		<category><![CDATA[hayman capital]]></category>
		<category><![CDATA[hedge funds]]></category>
		<category><![CDATA[kyle bass]]></category>
		<category><![CDATA[Michael Lewis]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=445</guid>
		<description><![CDATA[Today I thought I’d write about Kyle Bass. He is a favourite of mine and I love his clear no-nonsense approach and explanations of his world view. I have been following Kyle for about six months and he is certainly a character. I have seen him on various financial chat shows and a crazy interview [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=445&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I thought I’d write about Kyle Bass. He is a favourite of mine and I love his clear no-nonsense approach and explanations of his world view.</p>
<p>I have been following Kyle for about six months and he is certainly a character. I have seen him on various financial chat shows and <a href="http://news.bbc.co.uk/1/hi/programmes/hardtalk/9639507.stm">a crazy interview on BBC</a> where the presenter who clearly hadn’t got a clue about world markets just spent the whole time trying to make him out to be a big bad hedge fund villain. The best bit being his ‘good bye’ at the end.</p>
<p>His latest <a href="http://www.valuewalk.com/2011/11/kyle-bass-q3-shareholder-letter-imminent-defaults/#.TuO-ZmNyIRo">shareholder’s letter</a> which  pretty much sums up his position covers a lot of my own beliefs about Euro and Japanese Debt. I am not convinced we will see a war which will affect us in the west, but given Europe’s history of fighting amongst them nothing would surprise me. The Syria problem is also slightly worrying. Some people on ZH are calling this the US / European plan B.</p>
<p>Kyle is also mentioned in the book Boomerang by Michael Lewis, which is an entertaining read about how different countries got into the mess they are in now. Would have been good if this book had covered more countries, like the UK, France and Italy and Japan.</p>
<p>Kyle Bass, is the founder of Hayman Capital in Dallas. What I like about him is the way he researches his market view. He gets a view and then researches it and then he goes out and talks to all the people involved to make sure he has the right facts, including Congress, The Fed , Japanese officials etc.</p>
<p>If I had a Hedge Fund I would have a similar approach.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/445/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=445&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/10/hedge-funds-legends-in-their-own-right/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Making WPF Converters easier to use</title>
		<link>http://codingpowers.wordpress.com/2011/12/05/making-wpf-converters-easier-to-use/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/05/making-wpf-converters-easier-to-use/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 14:32:16 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Problems and Solutions]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Adapter]]></category>
		<category><![CDATA[IMultivalueConverter]]></category>
		<category><![CDATA[IValueConverter]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=435</guid>
		<description><![CDATA[When binding data to WPF controls it is good practice to use a IValueConverter or IMultiValueConverter or use the adaptor pattern which is what I do for the equivalent when using MVC for the web, to change the values from your domain objects to something you can bind to which makes sense for the Gui [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=435&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When binding data to WPF controls it is good practice to use a IValueConverter or IMultiValueConverter or use the adaptor pattern which is what I do for the equivalent when using MVC for the web, to change the values from your domain objects to something you can bind to which makes sense for the Gui controls.</p>
<p>I have recently been playing around with this concept and have come across a cool trick to save typing and make the XAML a bit tidier.</p>
<p>The problem with using IValueConverter for example is that you have to add the class to the resources in the XAML and then reference it with a string key. This is a bit clunky in my opinion.</p>
<p>If I forget to put the resources in I get the error I am trying to use my binding like an Extension property. It then came to me why don’t I make it an extension property.</p>
<p>So I now have a base class that all my converters derive from as follows:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">public abstract class BaseConverter : MarkupExtension </code><code style="display:block;margin:0 0 0 5px;">{
 public override object ProvideValue(IServiceProvider serviceProvider)
 {
 return this;
 }
}  </code><code style="display:block;margin:0 0 0 5px;">
</code></pre>
<p>I can now reference my converter from XAML like this:</p>
<pre style="font-size:12px;background:#ffff99;border:#ffcc66 solid 1px;overflow:auto;"><code style="display:block;margin:0 0 0 5px;">  
&lt;Label Content="{Binding Task1,Converter={Converters:TaskConverter}}"/&gt;
 </code></pre>
<p>A much cleaner solution.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/435/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/435/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=435&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/05/making-wpf-converters-easier-to-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
		<item>
		<title>Interlocked class for doing thread safe simple calculations</title>
		<link>http://codingpowers.wordpress.com/2011/12/04/interlocked-class-for-doing-thread-safe-simple-calculations/</link>
		<comments>http://codingpowers.wordpress.com/2011/12/04/interlocked-class-for-doing-thread-safe-simple-calculations/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 13:24:50 +0000</pubDate>
		<dc:creator>mrsimonpowers</dc:creator>
				<category><![CDATA[Back to basics - C#]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[Interlocked]]></category>
		<category><![CDATA[full memory barrier]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[memory fence]]></category>

		<guid isPermaLink="false">http://codingpowers.wordpress.com/?p=433</guid>
		<description><![CDATA[I came across the Interlocked class today. It is a very simple way of incrementing or decrementing or doing thread safe algorithmic calculations based upon a single property change in the same class. It is no good for changing the properties of another class, such as an element on a domain object, but is quite [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=433&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I came across the Interlocked class today. It is a very simple way of incrementing or decrementing or doing thread safe algorithmic calculations based upon a single property change in the same class.</p>
<p>It is no good for changing the properties of another class, such as an element on a domain object, but is quite good for incrementing a counter for example.</p>
<p>The methods on the Interlocked class:</p>
<p>Interlocked.Add()</p>
<p>Interlocked.CompareExchange()</p>
<p>Interlocked.Decrement()</p>
<p>Interlocked.Exchange()</p>
<p>Interlocked.Increment()</p>
<p>Interlocked.Read()</p>
<p>It is quite limited in its usage. The example for usage is given by MS as an <a href="http://msdn.microsoft.com/en-us/library/801kt583.aspx">incremental counter</a>.</p>
<p>These methods are supposed to be better performance than the using lock(), but the Exchange methods use a ref variable and so this is a limitation.</p>
<p>Worth knowing about for higher performance threaded algorithmic calculations though.</p>
<p>Amendment: 7/12/2011: I realised what these are for today. They are avoiding the double write / read of 64 bit integers on 32 bit systems when performing increment, decrement, addition or multiplication on Int64 types. You can use lock to do the same thing, but these methods do not use the blocking  Full Memory Barriers / Fences that Lock uses, hence their increased performance.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codingpowers.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codingpowers.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codingpowers.wordpress.com/433/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingpowers.wordpress.com&amp;blog=11352264&amp;post=433&amp;subd=codingpowers&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingpowers.wordpress.com/2011/12/04/interlocked-class-for-doing-thread-safe-simple-calculations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dcc4ac75ec39f4d89402e6d3f54749e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mrsimonpowers</media:title>
		</media:content>
	</item>
	</channel>
</rss>
