S h o r t S t o r i e s

// Tales from software development

Archive for June 2009

"WaitAll for multiple handles on an STA thread is not supported"

leave a comment »

My previous post, Waiting on multiple threads, mentioned Joe Duffy’s approach to synchronising multiple threads using a single WaitHandle. I was in the process of implementing a multi-threaded class library at the time but decided to take a conservative approach and use a WaitHandle for each thread. This is a less scalable solution but wasn’t really an issue as I knew that my class library would never need to use more than 8 threads concurrently.

Initial testing of the library from a COM client revealed a problem that I hadn’t anticipated: the call to the multi-threaded library method resulted in an exception being thrown with this error message:

“WaitAll for multiple handles on an STA thread is not supported”

The problem is that calling WaitAll on the thread WaitHandles from an STA thread blocks the Windows message pump and cannot be allowed otherwise the application would freeze. A quick fix for my class library was to iterate the WaitHandle array that was being passed to WaitAll and call WaitOne on each WaitHandle.

A better long term solution is to use Duffy’s single WaitHandle solution.


Written by Sea Monkey

June 15, 2009 at 8:00 am

Posted in Development

Tagged with

Waiting on multiple threads

with 2 comments

I’m currently implementing an interface that needs to communicate with several servers each time its methods are called. To reduce the elapsed time required the calls to each server are made concurrently using a thread per server. So what’s the best way to wait for all the threads to complete ?

If you search the internet you’ll probably find code examples based on using a semaphore for each thread. When the thread is started the Semaphore’s count is set to 1 (a non-signalled state) and when the thread completes the count is set to 0 which puts the Semaphore into the signalled state. The Semaphore’s static WaitAll() method is used to wait for all the Semaphores to enter the signalled state.

If you know anything about threading then you’ll probably know that while you can use a semaphore in this way, a ManualResetEvent is a better match for what is required here. Each thread uses a ManualResetEvent which is initialised in its non-signalled state by passing false to its constructor. When the thread completes it puts the ManualResetEvent into the signalled state by calling its Set() method. As in the previous example, the WaitAll() static method of the ManualResetEvent class is used to wait for all the threads to complete.

The problem with both these approaches is that creating lots of WaitHandles (the object that underlies the implementation of Semaphores and ManualResetEvents) has a fairly high overhead. This might not be worth worrying about for a few threads but if you want a scalable solution then using a WaitHandle per thread isn’t a good idea.

Joe Duffy presents an elegant solution to this problem on page 368 of his recently published Concurrent Programming on Windows. The code uses a single ManualResetEvent that is initialised in its non-signalled state and a thread safe count of the number of active threads is maintained using the Interlocked object:

activeThreadCount = 0;
manualResetEvent = new ManualResetEvent(false);
foreach (string server in urls)
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(BackgroundWorkerDoWork);
    Interlocked.Increment(ref activeThreadCount);

When each thread completes it reduces the active thread count by 1 and if the counter has reached 0 then the ManualResetEvent is signalled by calling its Set() method:

if (Interlocked.Decrement(ref activeThreadCount) == 0)

Now the code that is waiting for the threads to complete only needs to call the ManualResetEvent’s WaitOne() instance method:


Written by Sea Monkey

June 9, 2009 at 7:00 am

Posted in Deployment

Tagged with

Moore's law: Laptops

leave a comment »

Over the past nine years I’ve bought several laptops from Dell. It’s interesting to look at how the specifications and prices have changed.

Moore’s Law makes the observation that, historically, computing power has doubled roughly every two years. Although Moore didn’t specifically state this, it’s generally considered that this doubling in computing power is delivered to the consumer for the same cost. So, roughly every two years we get twice the computing power for the same price. As manufacturers have sought to lower the entry cost of a computer they’ve flipped this around to offer the same computing power for half the price every two years.

As an example, here’s three of the laptops that I’ve purchased:

2000: Dell Inspiron 5000e / Intel Pentium 4 733Mhz / 256Mb RAM / 30Gb disk – £2200

2005: Dell Inspiron 9200 / Intel Pentium 4 1.6Ghz / 1Gb RAM / 60Gb disk – £1500

2009: Dell Vostro 1710 / Intel Core Duo 2.6Ghz / 4Gb RAM / 320Gb disk – £750

The correlation to Moore’s Law is striking. These three laptops were bought at intervals of approximately 4 years and each cost roughly half what the previous laptop cost and delivered twice the computing power, four times the memory, and at least twice the disk capacity.

The reduction in cost is particularly impressive when you consider the additional features that we take for granted on a modern laptop – high resolution screen, NIC, WiFi, memory card reader, etc. The Dell Inspiron 5000e was a premium laptop when purchased in 2000 but had no built-in network connectivity. That seems astonishing in today’s online world. A PCMCIA network adapter cost another £120. I replaced this in 2003 with a IEEE 802.11 B WiFi card that cost £40. A year later this was replaced with a IEEE 802.11 G card costing £20.

However, what Moore’s Law doesn’t tell us is exactly how manufacturers achieve the reduction in prices. The latest laptop is probably the least well built of the three and the keyboard is amongst the worst I’ve ever used. Perhaps this is a slightly unfair comparison as the Vostro is Dell’s newer range targeting the home and low-end business market but it must reflect, to some extent, the fact that it’s just not possible to build a laptop to the same quaility level when you halve the retail cost every couple of years.

Written by Sea Monkey

June 5, 2009 at 7:00 am

Posted in Comment

Tagged with

Virtual Server 2005 R2 and Internet Explorer 8

with 2 comments

A couple of weeks ago I praised Microsoft for implementing a change in the .NET 3.5 SP1 framework that allows .NET executable assemblies to be run directly from a network share. I should have known I was tempting fate and sure enough a few days later I was shouting obscenities at the monitor of one of my servers because of a classic Microsoft cock-up.

The server in question is a dedicated VM host running Virtual Server 2005 R2 SP1 under Server 2008. Late last week Windows Update installed Internet Explorer 8. I didn’t notice any problems for a day or two because I was using another PC and Internet Explorer 7 to access the Virtual Server Administration Web Site. But on Wednesday afternoon I was doing some VM maintenance on the server using IE8 and the administration web site and I realised that all was not well. Clicking on the option to merge undo disks resulted in nothing. I tried a few other options like starting up a VM. Nothing. Then I noticed the error icon at the bottom left of the IE8 window and an error message: “Error on page”. 

I tried closing IE8 and restarting it with the same results. A quick search on Google revealed the answer – you need to enable IE8’s Compatibiilty Views feature for it to work with the Virtual Server Administration Web Site. Wonderful – Microsoft have managed to produce a version of Internet Explorer that isn’t even compatible, by default, with its own products.

Written by Sea Monkey

June 3, 2009 at 7:00 am

Posted in Comment

Tagged with

Life outside the Microsoft bubble

leave a comment »

I started working for a new company about three weeks ago and it’s been a minor culture shock. Unlike every company I’ve worked for in the past 15 years this one isn’t fundamentally bound to Microsoft tools, technologies, and platforms. That said, the company was interested in my skills and experience in exactly these areas.

A few examples of the differences: the ‘default’ database server for application development is MySQL 5.x instead of SQL Server 2005/2008; SharpDevelop is the standard development IDE; and Thunderbird is used in preference to Outlook.

The most surprising thing to me though is just how capable and mature these products are. MySQL 5.x is simple to install, configure, and use, and the GUI Tools are on a par with SQL Server’s Management Studio. I’m still not a great fan of SharpDevelop (fortunately the company has a few MSDN licences which means I’m still a Visual Studio user) but it’s a very usable and capable tool. Likewise, Thunderbird 2.0 is far from perfect but it’s still a very capable and mature application. 

The company specialises in developing applications in the healthcare sector and is very aware of the sensitivity of its clients’ data. Even anonimised test data must be carefully protected. The company has standardised on TrueCrypt for data encryption and, again, I’ve been very impressed with the maturity of version 6.x. About 6 years ago I checked out most of the ‘encrypted volume’ type products then available and chose to pay for SoftWinter’s Sentry 2020 rather than use TrueCrypt for free. But TrueCrypt has moved on while Sentry 2020 hasn’t. I’m happy with Sentry 2020 and like the fact that the product is stable but TrueCrypt has introduced a few features like hidden volumes and traveller volumes that Sentry 2020, as a commercial product, should probably have implemented some time ago.

The revelation that there is some really good software out there for free has got me checking out possible replacements for at least some of the software that I’ve been taking for granted. So far, I’ve only got as far as looking for replacements for Microsoft OneNote and the application that comes closest is EverNote. EverNote is a good product but it’s made me realise just how good OneNote is. The main problem with OneNote is that people who use it tend to evangelise it and use it for all their notes while those who don’t use it don’t even install it despite it being included in various editions of Microsoft Office. This makes it difficult to exchange OneNote data with other people. I can’t believe it’s a big earner for Microsoft and it’s a shame that they haven’t bundled it with Windows.

EverNote is worth a checking out if you’re looking for an application to store your notes. It’s a different product to OneNote and the two are to some extent complimentary. EverNote can be used as a standalone product but it’s intended to be a client application for EverNote’s web storage. So, you create your notes, synchronise with the EverNote web site, and then your notes are available from any other PC with the EverNote client installed or using the EverNote web site directly. What would be really nice would be OneNote’s document structure and interface, and EverNote’s storage model. 😉


Written by Sea Monkey

June 2, 2009 at 7:00 am

Posted in Comment

Tagged with