My first WPF 4.0/Windows 7 multitouch application

So I finally bought an Acer T230H multitouch enabled monitor that is supported by Windows 7. Actually, it is a dual touch but that’s enough. (for more on multitouch input devices for Windows 7 see my previous article).

On the good side it is a decent 1900x1080 monitor, not too expensive and multitouch works even under VMWare Workstation 7. On the bad side I knew it has some problems following fingers. Actually sometimes it gets just confused. That’s not a problem for a project I am working on but nevertheless I was curious.

Hence I created my first multitouch application that visualizes touch positions from monitor. Here is a screenshot featuring two fingers:

image

The application itself supports two different colors because I was interested only in two (no problem adding more if somebody wants me to). So, try the application and see how good or bad does your multitouch input device. Note, .net framework 4.0 RC is required.

As per my Acer T230H: indeed it has problems that usually manifest when fingers are nearby. And sometimes it just gets confused. Heck, it is one of the first multitouch monitors and a cheap one.

Have fun multitouching!

Enabling and understanding code contracts in Visual Studio 2010 beta 2

Code Contracts are a new way to do both runtime and static checking of your code coming with .net 4.0/Visual Studio 2010.

Prerequisites

Visual Studio 2010 beta 2. To enable Code Contract project tab you need to update code contracts by downloading the latest version from Code Contracts web page. You’ll find very good introduction documentations on the same side from Documentation link (PDF).

Here is a really simple example of a code that uses Code Contracts

static void Main(string[] args)
{
    decimal c = Divide(4, 0); // fails validation because b == 0
    List<int> list = null;
    Add(list, 5); // fails validation list == 0
}

static decimal Divide(decimal a, decimal b)
{
    Contract.Requires(b != 0);
    return a / b;
}

static void Add(IList<int> list, int element)
{
    Contract.Requires(list != null);
    Contract.Ensures(list.Count == Contract.OldValue(list.Count) + 1);
    list.Add(element);
}

Pay attention to Contract static class and its Requires (pre-condition) and Ensures (post-condition) methods. There is lot more to these two methods i.e. you can even access original argument values through OldValue method – make sure you read the documentation.

Just the code by itself won’t do much. To enable both runtime and static checking you have to open project properties and check Perform Runtime Contract Checking and Perform Static Contract Checking checkboxes. If the static analysis has been checked then you’ll see warnings in the Error List (and Output) window regarding contracts failures. If you enable runtime checking you’ll get assertions (by default, but you can easily switch to exceptions) when the program execution hits contract checking failure.

The interesting aspect of Code Contracts is how it implements post conditions – it injects the proper code at the place of the post condition methods. Something like Post Sharp has been doing for years. Looks like AOP is slowly getting into the .net after all.

Try it for yourself.

Want to try Parallel Extensions on .net 3.5?

Check out Reactive Extensions to .NET (Rx). Looks like it includes “a back ported (and unsupported) release of Parallel Extensions for the .NET Framework 3.5 in the form of System.Threading.dll”. So, if you don’t have Visual Studio 2010 beta handy you might check it out and let us know how it goes. While you are there make sure you check out Rx as well as it looks an interesting and useful library once you grasp its concepts.

See the related blog post.

What’s new in the BCL in .net 4.0 beta 2

Check out this post about what’s new in the BCL in .net 4.0. beta 2. As you can see there is a good amount of timesaving functionality.

My favorites are Stream.CopyTo, String.Concat/Join overloads that take IEnumerable<T> (no more casting IList to Array!) and of course Enum.HasFlag which I already implemented it my way.

I wonder though why they created Environment.Is64BitProcess and Is64BitOperatingSystem. At the present time it makes sense but Microsoft is already talking about 128 bit Windows OS. We’ll get, I suppose, a couple of years after a 128 bit Windows is released new properties: Environment.Is128BitProcess and Is128BitOperatingSystem and years after that perhaps 256 bit versions of them.

So, wouldn’t it be easier to have an enum like:

enum Bitness
{
    16Bit,
    32Bit,
    64Bit
}

And when 128 bit version comes out a new value of 128Bit might be added. So we might have Environment.ProcessBits and Environment.OSBits or something that would return Bitness enum, would be future proof and easier to use than inspecting all IsXXXBitXXX methods.

Or perhaps, if adding a new value to an enum poses some kind of problem, just return an int instead of an enum representing the number of bits.

Visual Studio 2010 beta 2 and .net 4.0 beta 2 available on MSDN

Both Visual Studio 2010 beta 2 and .net 4.0 beta 2 are available for MSDN subscribers and on Wednesday for everybody. Perhaps an important feature is that a “go live” license is available.

Release has been announced for March 22th next year. I guess RTM will be available before the year’s end for MSDN subscribers – judging from the Visual Studio 2008 timeline.

Here is a bounch of useful beta 2 launch blog posts:

About TFS 2010

Hanselman’s post

Jason Zander’s post

What’s new for the Task Parallel Libary

ScottGu’s post

and of course Somma’s post

Just don’t forget that this is beta 2 release and you should be cautious – installing it in a virtual machine rather then production one is always a good practice when dealing with early builds.

Dealing with iterations over null lists in LINQ to Objects

Problem

If you used LINQ to Objects you have certainly come across iterations over null values resulting in an ArgumentNullException being thrown at you.

See this example:

int[] b = null;
var query = from i in b select i;

In the case above the error is obvious but if the list comes as an argument of a method or some more complicated way it is hard to spot it. Sure, the obvious solution is an easy one. A simple if statement before will do it:

if (b != null)
    var query = from i in b select i;

What about nested loops? Like this

class Tubo
{
public List&lt;string> Strings;
}
...
IList&lt;Tubo> tubos = ...;
var query = from t in tubos
from s in t.Strings
select s;

Sure, we could add guardian clauses like the if before:

if (tubos != null)
     var query = from t in tubos
        where t.Strings != null
        from s in t.Strings
        select s;

The problem with this approach is that it gets cluttered and it complicates the flow, specially the first if.

Solution

So, here is my proposal and I am sure it has been proposed before (I just couldn’t find it on Google, err, I mean internet).

public static class Extension
{
public static IList&lt;T> Safe&lt;T>(this IList&lt;T> source)
{
if (source == null)
return new T[0];
else return source;
}
}

The extension method makes sure that query always gets a non-null list by making an empty one if it is null. The trick with extension method is that they can be invoked on null values which are passed as this argument.

And the last nested LINQ to Objects loop would look like:

var query = from t in tubos.Safe()
             from s in t.Strings.Safe()
             select s;

I am not sure that the extension method name Safe is adequate or not but it sure does help in code readability.

What do you say?

My favorite features in upcomming Visual Studio 2010/.net 4.0

The following two are my favorite features comming with .net 4.0 and Visual Studio 2010:

  • parallel extensions - a great and easy way to utilize those idle cores in your multi core machines. It might be as easy as adding an AsParallel() extension method to a LINQ query. I even did a bunch of presentations on the topic, the last one at NT Konferenca 2009.
  • code contracts - improving the code in both design time (static analysis!) and runtime (note: albeit this feature is a part of .net 4.0 beta you have to download the package to install design time support for Visual Studio 2010 beta. This step is supposed to be redundant for RTM or sooner).
Perhaps there are other better features however these two are the ones I am most interested *currently*. So, what are yours?

Running Visual Studio 2010 beta under VMWare Workstation 6.x

There are some graphical problems, probably due to new Visual Studio’s WPF UI, when running shinny new Visual Studio 2010 beta under VMWare Workstation: things like menus disappearing, strange artifacts appearing, etc.

Luckily the solution to this is very simple:

  1. make sure the virtual machine is turned off
  2. open virtual machine settings, go to Hardware tab and select Display
  3. Turn off Accelerate 3D graphics (DirectX 9.0c) option and that’s it.

disable

Picture quality might be a bit lower but at least everything will work. Btw I’ve installed Visual Studio 2010 in both Windows XP x86 and Windows 7 x64 virtual machines and it works just fine when graphics acceleration is turned off.

Update 9.12.2009: Workstation 7 supports WPF/Aero with hardware acceleration.