Category Archives: tech

If you’ve done any GUI programming at all, you’ll have come across a pattern like .NET’s System.ComponentModel.ISynchronizeInvoke. The problem it solves is fairly simple, yet only appears in a multi-threaded environment.

There’s one thread (it’s usually the first one created when the application starts, or at least the one that calls the entrypoint) that is responsible for creating and re-drawing UI controls. It handles messages from the Message Pump, and generally ensures that your UI looks as it should. It’s generally called the GUI Thread.

You can see the actions of the UI thread in Visual Studio. Create a Windows Forms project, and have the eventhandler for a new button throw an Exception. Also, create an event handler for the UnhandledException event on System.AppDomain.CurrentDomain, containing the following code:

MessageBox.Show(((Exception)e.ExceptionObject).StackTrace);

Then just run the app and hit the button. You should see the GUI thread being created, along with a marshal from unmanaged to managed code for the button click. Anyway…

There’s a simple rule: you can only update the UI from the GUI thread. The reason for this is simply explained by example: imagine if you were able to add a list item from one thread, whilst you were moving the window, and the window was being re-drawn from another thread. You’d have half-drawn list items all over the screen, and no consistency in your UI.

The rule is simple, but not usually enforced for performance reasons. Luckily, since .NET 2.0, you’ll get a ThreadAccessException (or similar) when you do this when a debugger’s attached. Back in the bad old days of .NET 1/1.1 you’d sometimes get a Big Red Cross where a control should be. Sometimes you wouldn’t. It’d most often occur once you’d been through UAT and you application was in production.

So what’s the solution? You need to marshal to the GUI thread when you’re not on it, update the control, then get back sharpish. Luckily, System.Windows.Forms.Control implements ISynchronizeInvoke, which provides some stuff that’s interesting to us. This is the bit that most people will be familiar with, but for those that aren’t, here’s the summary:

  • bool InvokeRequired { get; } – Gets a value indicating whether the caller must call Invoke when calling an object that implements this interface.
  • Object Invoke(delegate method) – Synchronously executes the delegate on the thread that created this object and marshals the call to the creating thread.

Simply put:

protected void Update()
{
  if(this.InvokeRequired)
  {
    this.Invoke(new MethodInvoker(this.Update));
  }
  else
  {
    textbox1.Text = "foo"; //whatever
  }
}

Yes folks, that’s another nice bit of boilerplate code that you’ve got to put all over your GUI code. Not pretty. But it’s alright, really, because I don’t do that much GUI code – I’m an infreastructure developer. Just kidding – though I did once refer to GUI programming as ‘painting by numbers’, which didn’t go down at all well with some people at work.

Anyhow, long story short: I found this cool thing that lets you use C# 3’s lambdas to describe the work you want to do on the GUI thread.

In C++, you can have a static variable defined in a function – scoped to the block, and with no linkage, but with a lifetime that extends from the first instantiation to the end of the program, unlike an auto. You’re not allowed this in C# – why not?

Well first of all, is it a good idea?

Static is one of the most overused and poorly understood keywords in the programming language ecosystem. In C++ alone it has at least five meanings – as a modifier affecting duration and linkage, orthogonally. It gets worse, because it was then co-opted to mean ‘determined at compile time rather than at runtime’.

In C++ static locals are commonly used as reference counters – initialise the counter once and increment it each time the function’s called. This can be replicated in C# by moving the static variable to class scope, but that feels like it breaks encapsulation.

Does it? No, it doesn’t. In OO, functions were never meant to contain data, only to expose and mutate it. A class or a struct is a container of data, so the class level is exactly the right place for your static.

Further, their use is actively dangerous, for reasons that aren’t immediately obvious. Functions with static locals are implemented with a hidden static boolean variable representing the initialisation state of the variable in question, as well as some simple code to check the state and instantiate it once. Predictably, this code itself isn’t thread safe. This issue is discussed at length in this codeguru article.

So, static locals aren’t really a very good idea in an OO environment, and also lull the unknowing developer into a false sense of security. Sounds like a bad candidate for a language feature to me!

Anders Hejlsberg didn’t take up my suggestions for new language features in C# 4.0, damn him. Instead we’re stuck with the moronic dynamic, a nod of the head towards stupidity and indolence.

The idea is that it’ll be easier to do some things which are currently fairly difficult, like interop with COM, and with dynamic languages like IronPython and IronRuby. In actual fact, the huge hidden cost is runtime initialisation of the whole DLR, which is not insignificant.

I quite admire the DLR, although (again) Microsoft have taken all the credit for clever techniques which are, in actual fact, quite old. Watch them do the same with HPC, MPI, and hosted applications (the cloud, whatever that means).

I won’t start a tirade about dynamic languages – they are what they are. But let’s not confuse ourselves: the words dynamic and huge runtime cost should be seared onto a developers’ mind next to each other. Any value whose type can’t be determined at compile time must, by definition, be determined at runtime. A compiler’s raison d’etre is to defer that cost – so beware, if you value productivity over speed of execution.

I still hold out hope for language support for ad-hoc tuples, although there are certainly strong arguments against their use. Members are anonymous, so they make a public API  confusing. There are constructs for structured data already – the clue’s in the name. The language is already big and convoluted, and there are other .NET languages that support them – C# shouldn’t be a panacea.

Get rid of var – that’d be the best thing. Or restrict its use so that lazy developers can’t use it to replace string or (why?!) int.

Hosted wordpress blogs allow reasonably rich text editing features, which I tend not to use. They don’t always translate well to places other than the website, and aren’t really guaranteed to work on other platforms.

For example, I use NetNewsWire Lite as an RSS aggregator on my mac. You usually just get the text there, so a special point made by using a different font or a fancy effect would be lost on me. Think of readers with blackberries, iPhones and older devices when you choose your style: I prefer to cede control to the great WordPress gods, and blame them when they get it wrong (-:

Anyway, one exception I’m happy to make is source code. It looks terrible in a variable-width font with no syntax highlighting, so any way to make code easier on the eye is something I’ll lap right up.

WordPress has a smart tag called ’sourcecode’ (imaginatively enough!) with a required ‘language’ argument, which can be one of cpp, csharp, css, delphi, html, java, jscript, php, python, ruby, sql, vb, or xml.

Unfortunately, there’s no ‘FSharp’ option (maybe someone should suggest it – surely it can’t be that complicated to implement?), so none of the options really fit. At least it’s a fixed-width font, though, and is a little easier on the eye. After a little experimentation I’m happiest with csharp, as it knows about primitive types and doesn’t get things wrong as much as the other settings do. Here’s an example:

#light

let getHostPort servicename =
  let host, port = serviceBroker.GetDetails serviceName
  host, port

Here’s a link to the formatting instructions.

Hope that helps!

A perennial question on messageboards, BBSs, and mailing lists of a certain kind is “what music do you tend to listen to when coding?” I’m a fairly lazy listener, so my answer tends to be “whatever’s on”. But Apple’s Genius Playlist feature has revolutionised the way I listen to music.

I’ve never really bothered with manual playlists – they take too much time to create, and have a short half-life before you get bored with them. The recently added smart playlist in iTunes is useful, because I often want to lsten to a new album the whole way through. But I reckon that’s a fairly uncommon desire. Unfortunately, because of the way the menus on your iPod are laid out, it’s quite often what people end up doing.

Genius playlists allow you to choose a track from your collection that suits your mood. It’ll then generate an ad-hoc playlist of twenty-five tracks from your collection that match the choice you made. The result is a couple of hours’ listening that matches your mood. It works on iTunes on your Mac or PC, and on your iPod. You can save generated playlists for later, and regenerate one if it’s not a very good choice.

I don’t know how Apple generate the track listing. When you first switch on the feature iTunes spends some time analysing the tracks in your collection, and talks to Apple about what you’ve got. It doesn’t seem to do any ‘rights’ analysis to see if you actually own the music in your library.

The thing is, the technology isn’t new. Audio analysis has been around for ages in the form of voice commands for computer systems (extant, but clearly not ubiquitous). British readers can use Shazam – call 2580 from your mobile phone, and the computer on the other end will listen to the music in the background for a little while, hang up, and text you back with the name of the track and the artist within a couple of seconds. It’s great fun to figure out how it works.

Amazon.com have been suggesting books you might like for years – they generate links between products based on what other people have looked at and bought. It’s a fairly good metric. Spam filters have been analysing documents for similar characteristics, although they generally only have two buckets for the results – spam, and not spam. Google Sets understands the links between collections of things – enter ‘John’, ‘Paul’, and ‘George’, and Google will tell you that the missing member of the set is ‘Ringo’ (the miserable sod).

The spam filter is the odd one out here. The cost of a false positive for Google Sets, Amazon’s recommendations, and Genius playlists is fairly low. Google don’t lose any money if they mistakenly thought you were naming the apostles. If you don’t take Amazon’s advice it’s really no skin off their nose (and if you buy something else instead, guess what? They get another data point and do better for the next guy). You can regenerate a Genius playlist you didn’t like, and what’s more: there’s a way that you can improve the results.

As well as showing a list of matching tracks in your collection, iTunes will also download a list of similar songs that you don’t own, and show you how you can buy them from the iTunes music store. All of these systems benefit better from more data. Google has plenty, so does Amazon. If you don’t have enough music in your iTunes library, it’s not Apple’s fault!

So, how might Apple be generating those track listings? I’ve got a couple of ideas:

  • They might know the contents of peoples playlists. Assuming that tracks in a single playlist are similar, they’d create a weighting between those tracks.
  • Similarly, they could do the same with tracks people bought in a single iTunes Music Store session.
  • They might put a lesser weighting on the sum total of all the tracks that a single person has bought in the iTunes Music Store, based on the assumption that generally, different people listen to different kinds of music.
  • They might do audio analysis on your music (or buy the data off someone else) – BPM, key instruments, genre, style, timbre, etc.
  • They clearly know what music a person has in their iTunes library. I’d expect that they’ve got dynamic statistics too, like how highly people have rated various tracks, and how many times they’ve listened to a track. All this can be aggregated together, too.

Lots of those things seem ‘hand-wavey’, but bear in mind that iTunes has millions of users and assumptions based on aggregations of huge data sets often work. Ask yourself what constitutes a false positive entry in a playlist? And who decides?

This is one of the prettiest things i’ve seen on the web in quite a while:

The team from Idée Inc. use Flickr’s API to generate a large corpus of ‘interesting’ images. Then they filter based on colour to show similar images. Further, their technology ‘Piximilar’ finds images that look like an image that you select.

I’ve done some work in the past with Media Bin from Interwoven, and they had a similar ’search by colour’ feature. It’s great for brand images, and the results get better and better as you add more images. That’s why it’s fantastic with a huge body of free images like Flickr.

All of my pictures on flickr are licensed under Creative Commons – not because of any ideological feelings (I don’t have a problem being paid for work I’ve done), but because I like using other peoples’ photos when I can, and that system only works if most people abide by the rules.

I recently did this, and it’s a little convoluted.

I don’t have an external CD drive, or a >1Gb flash drive, so I thought I’d give netboot a go. Here’s what to do:

  1. On your ibook, start the tftp server:
    sudo /sbin/service tftp start
  2. Download and extract the pxe image to your tftp folder:
    wget http://archive.ubuntu.com/ubuntu/dists/hardy-updates/main/installer-i386/current/images/netboot/netboot.tar.gz
    tar -xvzf netboot.tar.gz -C /private/tftpboot
  3. Download, compile and run dnsmasq, a DHCP server:
    sudo fink install dnsmasq
  4. Add the following to /etc/dnsmasq.conf:
    user=root
    resolv-file=/tmp/resolv.conf
    no-poll
    interfact=en0
    dhcp-leasefile=/tmp/dnsmasq.log
    dhcp-range=169.254.32.0,169.254.32.100,86400
    dhcp-option=3,169.254.32.135
    dhcp-boot=pxelinux.0
  5. You’ll need to change some of those bits. First of all, the interface name should match that of your iBook’s built-in ethernet port. Run `ifconfig` in a terminal window to find that out. Whilst you’re there, find out what IP address has been assigned to it, and slip that into the dhcp-option line. Then change the range to match the first three parts of your IP address – this is the range of addresses that your DHCP server will choose from when your Aspire asks for an address when it boots up.
  6. Switch on the DHCP server:
    sudo dnsmasq -C /etc/dnsmasq.conf

    Connect your Aspire and iBook together with an ethernet cable, and turn on your Aspire.

  7. Early in the boot sequence you’re given the opportunity to go into setup. Hit ‘F12′ and choose to boot from the network interface. Then save & exit. With any luck, your Aspire will ask your iBook for an IP address, be given one, and will load the pxelinux.0 boot file with tftp.
  8. If something goes wrong, there are a couple of things you can do. Check the screen for useful output (at this early stage of the game it’s not terribly likely that there’ll be much there, but you never know). Try to get the pxelinux file from another computer using a tftp client (there’s one build into OS X, certainly, and they’re easy to find on the net for other Operating Systems). Look in the dnsmasq log file for information. I was stuck at this stage for quite a while, and in my case the fix was to play with the range of IP addresses until I got it right.
  9. If all goes well, you’ll be prompted to ‘boot’. Just hit enter, and keep going through hardware and network configuration. The bootstrap installer will need to access the internet to download the rest of the install files, so select a mirror and watch it break.
  10. That’s right, it’ll probably break at this point. Remember how we gave the aspire an IP address to allow it to boot up? Well, it now thinks it can access the internet, and it probably can’t. So, switch off the DHCP server on your iBook
    sudo killall dnsmasq

    Then enable internet connection sharing on your iBook. In my case my internet connection was wireless, so I was fine. Otherwise, try plugging your aspire straight into the router or a switch, as long as there’s a working DHCP server on your network (it’s probably your router)
    System Preferences -> Sharing -> Internet

  11. Then, if you’re lucky, going back a few steps in the installer and re-auto-detecting network settings should be all you have to do. Your router should provide your Aspire with an IP address for the local network using DHCP, and you should be able to continue the download. That didn’t work for me, I’m afraid to say, so I had to manually configure the network settings for my laptop. Here’s how to do that:
  12. Ensure your Aspire is connected to a machine (or router) with internet access. In my case it was my iBook that was connected to an Apple Airport Express via wifi. Hit `ifconfig` in a Terminal, and find the interface (en1, in my case) that’s connected to the internet. Look for a line saying ‘inet’. Pick an IP address and Netmask based on the values in that line, though the IP address you choose probably shouldn’t be the same as any machine on the network.
  13. This did the trick, in my case. If not, go back and try auto-configuring the network again. Sometimes the manual changes you made will kick the thing into life. I know that sounds quite unscientific, but the whole thing seems like voodoo to me anyway.

At this point, you should be able to continue from step 3 (“Tweak/Fix”) on the Ubuntu AspireOne install page. Good luck!

(keep an eye out for my upcoming post ‘Is linux still not ready for the desktop?!’)