Thursday, May 13, 2010


Here's another handy class in the spirit of WeakEventHandler. It's useful when you want to run work on a background thread, but wish to provide a callback that will be run on your own (usually UI) thread. Apparently Silverlight users will find this particularly useful, there being no Application.Current.Dispatcher in Silverlight, but as an almost-exclusively WPF user, I use it as helpful shorthand for "call me back on my own thread".

The class:

public class CrossThreadAction<TData> : DependencyObject
Action<TData> target;

public CrossThreadAction(Action<TData> target)
{ = target;

private void Invoke(TData data)
Dispatcher.BeginInvoke(target, data);

public static implicit operator Action<TData>(
CrossThreadAction<TData> cta)
return cta.Invoke;

Use it as follows:

private void StartWork()
// using a server method with signature
// void DoWork(Action<string> callback);
server.DoWork(new CrossThreadAction<string>(FinishWork));

private void FinishWork(string result)
textBlock1.Text = result;

WPF users may make it slightly less heavy by inheriting from DispatcherObject instead of DependencyObject. Also, beware of storing an instance in an untyped variable; generally it's a good idea to cast it to Action<TData> as soon as you've created it.

Labels: ,

Friday, May 07, 2010

Electoral Reform in Britain

What’s wrong with the system

As anybody who has followed this election will have realised, it’s staggeringly unfair that the Liberal Democrats, with 23% of the popular vote, only got 8% of the seats. That’s not democratic, and it entrenches the two-party system. Do you often find it hard to distinguish between the respective policies of Labour and the Tories? You’re not alone.

The Lib Dems want a proportional representation system. However such systems have their own problems, the biggest being that MPs would be even less accountable to their constituencies than at present. Think safe-seat manoeuvres on steroids. I don’t think that will fly in a nation where constituents are used to being able to chuck out MPs they don’t like. MPs who are unpopular with the electorate will get in by virtue of their party’s popularity rather than their own. As Italians will attest, this leads to MPs being able to shrug off the most outrageous of scandals, provided only that they can make good with their party.

A sensible solution

Much better would be to implement democracy at a local level. Suppose that each constituency elected a small parliamentary council, proportionally representative of the candidates (a Single Transferrable Vote system would do very well for these councils). That parliamentary council would then elect a representative to sit in parliament, but – and here’s where the power really comes to the people – the council would have the power to instruct its representative on which way to vote. That means that votes in parliament would be on the basis of what the constituencies want, rather than what the party wants. Real democracy!

Why this will never happen

The problem is this: the parties simply won’t wear it. Being unable to whip their MPs into line, party policies would become not so much policies as guidelines. An MP might join a party to benefit from collective think-tanks on the issues, but the real power would be taken away from the parties and given instead to the constituencies. You like it, I like it, politicians hate it. After all, what’s the use in being Prime Minister if you can’t tell parliament what to do?

Nice thought, though

Credit for this idea goes, as far as I can tell, to Sir Antony Jay and Jonathan Lynn. Wouldn’t it be nice if politicians really did act in the interests of the people who elected them?