.NET 4.5 Baby Steps, Part 3: IDataFlowBlock
Introduction
A new interface in .NET is the IDataFlowBlock, which is implemented in many interesting ways, so to look at those we will start off with the simplest implementation. ActionBlock<TInput> is a completely new class in .NET 4.5 and provides a way of working with data in a very task orientated way. I simplistically think of this as the implementation of the IObserver interface we got in .NET 4. but do not limit your thinking to just that.
To use it, you first must add a reference to System.Threading.Tasks.DataFlow
In this simple first example I am doing a fairly simple Pub/Sub demo:
var subscriber = new ActionBlock<string>(input => { Console.WriteLine("Got: {0}", input); }); for (int i = 0; i < 10; i++) { Task.Factory.StartNew(() => { Thread.Sleep(new Random().Next(200, 1000)); subscriber.Post(DateTime.Now.ToLongTimeString()); }); } Console.ReadLine();
![CropperCapture[3] CropperCapture[3]](https://www.sadev.co.za/files/CropperCapture3_thumb.gif)
As IObserver<T>
So the first fantastic feature is that it does have the ability (via extension method) to be an IObsserver<T> so it really solves the need to build up your own subscriber classes when implementing a pub/sub model.
First is the code for the publisher class – this is normal for the IObservable<T> as we had in .NET 4. This just means our new code can play well with our existing code.
public class Publisher : IObservable<string> { List<IObserver<string>> subscribers = new List<IObserver<string>>(); public IDisposable Subscribe(IObserver<string> observer) { subscribers.Add(observer); return null; } public void Send() { foreach (var item in subscribers) { item.OnNext(DateTime.Now.ToLongTimeString()); } } }
For our demo code, which produces the same as above:
var publisher = new Publisher(); var subscriber = new ActionBlock<string>(input => { Console.WriteLine("Got: {0}", input); }); publisher.Subscribe(subscriber.AsObserver()); for (int i = 0; i < 10; i++) { Task.Factory.StartNew(() => { Thread.Sleep(new Random().Next(200, 1000)); publisher.Send(); }); }
Complete
The next awesome feature is the Complete method which can be used to stop accepting of input when called – this is great for services where you want to shut down.
In this demo code it will run until you press enter:
var subscriber = new ActionBlock<string>(input => { Console.WriteLine("Got: {0}", input); }); Task.Factory.StartNew(() => { while (true) { Thread.Sleep(new Random().Next(200, 1000)); subscriber.Post(DateTime.Now.ToLongTimeString()); } }); Console.WriteLine("Press any key to stop input"); Console.ReadLine(); subscriber.Complete();
.NET 4.5 Baby Steps, Part 2: Task timeout cancellation
Introduction
When Tasks where introduced in .NET 4 one of the fantastic abilities was to be able to pass in a CancellationToken and use that to cancel/break out of tasks (think like a cancel button on a file copy).
So in the following code we create a cancellation source and pass the token to the task and it will output the date/time until you press enter. Then we call the Cancel method and it stops.
var cancelTokenSource = new System.Threading.CancellationTokenSource(); Task.Factory.StartNew(() => { while (!cancelTokenSource.IsCancellationRequested) { Console.WriteLine(DateTime.Now.ToLongTimeString()); Thread.Sleep(1000); } }, cancelTokenSource.Token); Console.WriteLine("Press any key to cancel"); Console.ReadLine(); cancelTokenSource.Cancel(); Console.WriteLine("Done"); Console.ReadLine();
What is new in .NET 4.5?
.NET 4.5 adds a FANTASTIC new feature to this, the ability to cancel automatically after a set timeout! So all we need to is change the constructor and set the time out. In the demo below it is set to three seconds.
It is also important to note that it is time from when you create the token source and not time from when the task starts.
var cancelTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3))
Below in the screen capture, see how the word Done does not appear but processing stops? That is because it is cancelled (processing stopped) but I never pressed any keys – so it is still waiting for the readline above Done.
.NET 4.5 Baby Steps, Part 1: ThreadLocal<T>
Introduction
ThreadLocal<T> was introduced in .NET 4 and didn’t get much attention because it didn’t do much over the ThreadStaticAttribute which we have had since version 1 of the framework, so let’s just review what it does. In short it gives every unique thread that uses it, it’s own global field. Let’s look at this code:
static ThreadLocal<int> balances = new ThreadLocal<int>(() => { return 10; }); static void Main(string[] args) { for (int i = 0; i < 10; i++) { new Thread(AddMoney).Start(); } Console.ReadLine(); } static void AddMoney() { Console.WriteLine("Before {0}", balances.Value); balances.Value += new Random().Next(0, 1000); Console.WriteLine("After {0}", balances.Value); }
Which produces:
Note that ever Before is set to 10 and that is because the lambda method that we pass to the ThreadLocal<T> constructor is run for each unique thread.
What’s new in .NET 4.5?
.NET 4.5 improves the usefulness of this by including the .Values parameter which allows you to list the results from each thread! To make use of this you need to opt-in in the constructor by adding true:
static ThreadLocal<int> balances = new ThreadLocal<int>(() => { return 10; }, true);And then in my demo I will output the results using:
foreach (var item in balances.Values) { Console.WriteLine("Balance at end: {0}", item); }
![CropperCapture[2] CropperCapture[2]](https://www.sadev.co.za/files/CropperCapture2_thumb.gif)
This is VERY useful when working with threads and doing individual calculations and then collating the results at the end!
Warning
ThreadLocal<T> only works with unique threads! So using with the TPL or ThreadPool which reuse threads will not work as expected!
JSinSA 2012
This past weekend was the ever fantastic JavaScript in South Africa (JSinSA) conference. This year focus was on HTML 5, JavaScript & CSS 3 – easily some of the MOST important topics for developers regardless of platform to know about.
It was it’s second year and while I was very lucky to go to it as an attendee in the first year, this year I was even more lucky to be a presenter at the conference. I was also very lucky to present on a topic I am passionate about: Windows 8.
The talk provided an introduction to Windows 8 & how development works, and in the 45mins I was done, we built an application which could take a photo from a web cam and send it to Twitter (the actual photo is to the right).
You can get the slides and bits from the talk below.
South African Postal Codes for Windows Phone
I can NEVER EVER remember the postal code for where I work or where I live – that little four digit number just eludes my brain. So to solve that I thought, why can’t I have EVERY postal code with me always? So that is what I made happen with this simple Windows Phone application: Postal codes!
It is dog simple: one input the name of what you want then hit search and boom results. It includes both street and box codes
For the developers out there, this application source code is available too at: https://bitbucket.org/rmaclean/postal-codes
Windows 8 for the .NET developer
Last night I presented on Windows 8 for the .NET developer at the fantastic Developer User Group! We had a bumper crowd there which was great and really had some interesting discussions during and after the talk. Thank you to all that attended!
For those looking for the slides, demo script and demo bits they are below!
Why the harder you work to prove to Microsoft you know better, the less chance it will ever happen
Disclaimer: I do not work for Microsoft and these are my views based on discussions with multiple people at Microsoft which I have stitched together – maybe I misunderstood everyone and this is all wrong too. All examples I use are my own. I am no lawyer – check with a lawyer for legal & license advice.
tl;dr: Microsoft is really worried about being sued and thus is risk adverse to “stuff from the internet”. It is better to tell Microsoft what you dislike, not how to fix it. Learn about licensing content.
Paper Work
A few years ago I went on an amazing trip to work with Microsoft but before I could do that I needed to sign not only a NDA, but also waivers for the work I would do – which makes sense. I did it for free and Microsoft didn’t want me to sue them for money later for my work. Not only that I had to sign them, my employer had to do the exact same thing. Once again because I work for someone else who could claim money from Microsoft and Microsoft lawyers had deemed that a risk and needed to be protected.
This involved a lot of time and money, it is VERY expensive to have lawyers review documents from other lawyers and the DHL the originals half way round the world, but it is far cheaper than being sued.
I know that neither myself of BBD would sue Microsoft for the work I did, but that doesn’t still the hearts of those lawyers who live in a world of ugly mean liars that will cheat the system if it was easy and good. I wish it wasn’t this way but some wishes don’t happen.
The Users Voice
A while back Microsoft started spinning up loads of uservoice.com (UV) sites to collect feedback and I believe they are successful in getting some things changed. There is an odd issue I see on UV especially with how Microsoft deals with it, that being as technology advanced users & developers we are taught to give the most detail as possible – really there is nothing like too much detail… however in UV, it seems that Microsoft ignores them and favours those who do and give very little. A great example of this, is in Visual Studio land where we can compare the current top two ideas: this short idea which is “under review”
versus to this guy who has pages of details and even as taking the design and proving a lot of it could work – for all his hard work, nothing.
WTF?! Microsoft doesn’t listen to me
If you read both suggestions they seem to say the same thing except the lazy guys one got the reward, right? No – it is more fundamental than that. The first one is really just discussing the what & why the VS colour change that is an issue, the second piece of feedback though is discussing how to fix it. The problem for Microsoft is if they take the second guys stuff, a person who hasn’t signed a waiver, the how guy has a legal ability to sue Microsoft for the money they owe him for work/royalties etc… And Microsoft legal won’t allow that to happen because that is their job, to protect Microsoft legal issues.
This is not a complaint about legal, I am sure they are nice people that are just doing their job and it is annoying their job and my wishes do not align...
The thing about taking the what feedback is Microsoft is pretty safe in taking and improving VS in anyway they see fit and that is why the what & why is under review and not the how.
Licensing & Public Domain
The next that will be brought up is that this is work in the public domain and thus “free”… wrong. Public domain work is more a legal trap than anything, and there is so many steps that you need to jump through to get access to using that “free” work that often it is easier to redo it yourself. This is why ANYTHING you do should have a license, even if you want to just give it away and never see it again or if you want someone like Microsoft to be able to use it.
For software check out a good open source license, such as BSD 3-clause which basically says do what you like with my work and I promise I won't sue you except if you use me as an endorsement for your product which contains my work. For non-code items, like art, music or blog posts have a look at the creative commons licenses.
Microsoft can fix this too
Microsoft could reach out to people with good ideas and get them to sign waivers (WAY too much work and also maybe risky after the work is provided), but better would be to adopt an approach like StackExchange (SE) does. SE states if you provide feedback on their sites it is creative commons.
Microsoft could do the same and even put in a waiver clause on UV, I don’t know if UV allows for this, but Microsoft is big enough to get it done. It doesn’t solve great ideas that are posted elsewhere, those still required YOU to take the time to learn a little about licensing, public domain and so on and take the right steps so we can ALL benefit… not just the lawyers who get paid to say no.
IntelliTrace vs. PreEmptive Analytics
IntelliTrace
Visual Studio 2010 introduced an amazing feature: IntelliTrace which allows for deep debugging experiences inside Visual Studio by collecting an AMAZING amount of information (basically a stack trace for every call in your code + meta data) and allowing you to use it later to replay the way the application was used. With this feature you could eliminate those “No Repro” bugs! The catch in 2010 was it was NOT allowed to be used in production. In Visual Studio 11 that has changed and we can use it in production: http://msdn.microsoft.com/en-us/library/hh398365(v=vs.110).aspx & http://msdn.microsoft.com/en-us/hh440472
PreEmptive Analytics
This change in licensing may seem to put IntelliTrace in direct competition with another great tool, PreEmptive Analytics (PA). I have mentioned this amazing tool before and with Visual Studio 11 it is included “in the box” so there seems to be a conflict brewing – but there isn’t.
Two sides of the same coin
These two tools are both part of the collect information so you can react to it later and fix bugs set of tools, but they have very different use cases. IntelliTrace is specific to the scenario of replaying an application for diagnosis and debugging purposes. It is not meant to be an always on tool and it is a tool that writes to a local file that needs to be collected some how.
PA on the other hand is a tool to always have on, it does capture error information but nothing more than the simple Exception + Stack which is not as useful, detailed or integrated into VS when compared to IntelliTrace. In addition PA allows me to do a lot of a lot of analytics on my application that are not possible in IntelliTrace:
- what features are people using
- where in the world are they
- when are they using it
- what are their machines like
In addition the PA reports get automatically sent to a server (that they run or that you can run if you have privacy/security concerns) so there is not need to waddle around collecting files.
I can also see scenarios that these two work hand in hand – PreEmptive getting higher level info that shows a lot of users having issue X, then the support guys contact some users and do a more detailed capture of the issue with IntelliTrace.
South African ID Numbers: The racial identifier flag
Introduction
In a previous post on what makes up an ID number I mention that
The second last number used to be a racial identifier but now means nothing.
But I never went into the topic so lets dive into the options – today it is for almost everyone 08 (I suspect a 09 or two may be floating around) but in the “bad old days” there was a variety of options:
Population Group | S.A. Citizen | Non-S.A. Citizen |
White | 00 | 10 |
Cape Coloured | 01 | 11 |
Malay | 02 | 12 |
Griqua | 03 | 13 |
Chinese | 04 | 14 |
Indian | 05 | 15 |
Other Asian | 06 | 16 |
Other Coloured | 07 | 17 |
For my non-South African readers the use of Coloured as a group here is not the same as the American racial slur, in South Africa we have a population group called Coloured: http://en.wikipedia.org/wiki/Coloured
How did we change from the old to the new?
So what happened to those bits as we do not have them now? In 1986 there was the introduction of a new law: Identification Act no 72, which caused the law that made the classification (and a horrible concept where every black person had to carry a “Pass Book”) repelled.
So over the course of 1986 and 1987 everyone in South Africa was issued a new ID number and somewhere inside the government there is a database that maps old ID numbers to new ones for people born before 1986! I can’t remember what this was process was like, since I was about 4 years old at this point. This means though I have a different number on my birth certificate to what I use now!
.NET 4.5 and how it sits in the .NET ecosystem
tl;dr
- .NET 4.5 – 8th major release.
- .NET 4.5 is an in place replacement of .NET 4.0.
- Installing it could cause issues for .NET 4.0, but is very unlikely and likely shows your app is using undocumented features or using features incorrectly.
- .NET vesions, CLR versions & language versions are not in sync.
- There is an awesome chart below which makes it easy to see the relationships in the ecosystem.
Introduction
.NET 4.5 is the next release and it is important to take a look how it fits in the .NET ecosystem. This is the 8th major release of .NET! What do I mean by major release? I mean any release that is not a patch/support only release, or put another way a major release it included major new features in the CLR and/or a new language options.
SxS vs. Replacement
In .NET we are lucky that many versions can run side by side (SxS) provided they have different versions of the CLR, however if a new major release shares the same CLR it is a replacement/additive version. For example: .NET 3.0 used the same CLR as .NET 2.0 (the CLR 2.0) and when installed replaced many of the files in .NET 2.0 with new versions and it is only via compiler directives that some things are turned on and off. The advantage of the SxS model is installing a new version doesn’t influence apps on the previous version in any way (i.e. if the app is 1.1 and works fine before .NET 2.0 was installed, it will keep working fine after .NET 2.0 is installed).
The problem with replacement model is that there is a chance that installing a new version breaks apps on the original version – however Microsoft does a RIDICULOUS number of testing to make sure this doesn’t happen, so that chance is very small. In fact if you happen to hit one, the chance is higher you are using undocumented features or using features incorrectly.
The reason for this explanation of SxS vs. replacement is that .NET 4.5 is an in place replacement for .NET 4.
Version Naming
Part of the confusion I suspect around me saying that .NET 4.5 is the eighth release is because Microsoft naming of versions is about as far from logic as you can get – the worst examples are the .NET Version 3.5 SP 1 is a major release labelled a Service Pack 1?! and the fact we do not have a version 3 of the CLR, it was just skipped?!
The other aspect is that versions of the CLR, versions of the Framework and versions of the languages are completely out of sync, so .NET 4.5 runs on the CLR version 4 and we write code in C# version 5.0 or VB version 11.0 – cause that makes sense :S
Awesome Poster
Here is an awesome poster to help remind you of all the above!