Watch out for those WaitOne() overloads (when you need backwards compatibility)

Recently I was writing some new functionality that ran work on background threads.  The functionality isn’t that important but one of the classes I was using to get the correct behavior was AutoResetEvent.  Specifically the AutoResetEvent.WaitOne() method.

Since I really don’t use this class very often, I relied on intellisense to give the list of method overloads and I selected the WaitOne(Int32) overload which allows the thread to wait a given number of milliseconds unless signaled first.  My compile worked and the functionality ran properly in Visual Studio 2008 SP1.  Since I share the of the same code base when my product is installed in VS2005, I jumped over and the product compiled and ran perfectly there as well.

I forgot all about this until I was doing the final testing in preparation for a new release.  The first few VPC images took the installation and ran fine.  However, when I ran the installation in a VPC image that had a clean installation of VS2005 SP1 and nothing else, the product did not work properly.  I was puzzled until I enabled logging and found that I was getting a MissingMethodException on the WaitOne(Int32) method.

I’ll spare you the details of the agony but I thought about this for a while and decided to try installing the .NET 3.5 SP1 framework in this VM and, low and behold, the problem vanished.

It turns out this method overload was introduced in .NET 3.5 SP1 but because System.Threading resides in mscorlib and there is only a 2.0 version of that assembly, any .NET 2.0 application (which in my case was the VS2005 package) is able to compile against it and run as long as the SP1 version of .NET 3.5 is present on the machine.

The solution was to use the WaitOne(Int32, bool) passing false to the second parameter which gives the same functionality.

I’ll be watching for this type of thing a little closer from now on.

Comments

12 Responses to “Watch out for those WaitOne() overloads (when you need backwards compatibility)”

  1. Brian Moody on July 8th, 2009 2:15 pm

    You solved my problem. Thank you!

  2. Confluence: POS Development on October 6th, 2009 10:32 am

    WaitOne() overloads…

    Be careful using WaitOne in the System.Threading library. I got this error compiling against the .NET 2.0 framework without 3.5 installed. MptSession.cs(99,26): error CS1501: No overload for method ‘WaitOne’ takes ‘1’ arguments MptSession.cs(44…

  3. Lucas on November 16th, 2009 9:53 am

    Hi. Thanks for writing about this. I encountered this problem today, search for it, and found your article.

    I just wanted to add that technically what’s required to use this method is framework 2.0 SP2. If you have 3.5 SP1 installed, you already have 2.0 SP2 and 3.0 SP2 since they are prerequisites.

  4. Jiashu Wu on December 4th, 2009 12:31 am

    Thank you so much for sharing this knowledge and experience on Web. It saved my day!

    I am developing with Visual Studio 2008 with .NET 3.5 SP1 installed. The application is supposed to be installed on XP, Vista or Windows 7 of both 32 and 64 bit machines without requiring .NET 3.5 SP1. The System.Threading.AutoResetEvent.WaitOne(int32) overload works happily on my development machine, as well as testing machine with 32 bit Windows. However on testing machine with 64 bit Windows I got the MissingMethodException. Your article provided exactly the right solution for my problem.

  5. BacchusNaur on March 3rd, 2010 7:14 am

    Thanks for this article. It described the precise issue I was facing. Funny how the smallest one-off programs cause the biggest issues.

  6. Adonis on March 11th, 2010 2:50 pm

    Thank you so much for sharing this knowledge and edperience on Web. It saved my day!

    I am developing with Visual Studio 2008 with .NET 3.5 SP1 installed. The application is supposed to be installed on XP, Vista or Windiws 7 of both 32 and 64 bit machines without requiring .NET 3.5 SP1. The System.Threading.AutoResetEvent.WaitOne(int32) overload works happily on my development machine, as well as testing machine with 32 bit Windows. However on testing machine with 64 bit Windows I got the MissingMethodException. Your arficle provided exactly the right solution for my problem.;

  7. Alan Singfield on March 24th, 2010 4:27 am

    Thanks – this solved my problem too.

    In my case I was using the SmartThreadPool library and this is using the overload you mention.

    It actually fails to start threads in a random fashion when run on 3.5 without the service pack; no errors or warnings occur.

    Hopefully this will save other people tearing their hair out!

  8. BacchusNaur on April 14th, 2010 8:08 am

    Ditto on SmartThreadPool.

  9. me on May 11th, 2010 3:43 am

    Thank u very much, I encounted this problem too. and now solved

  10. dcx2 on May 18th, 2010 11:42 am

    This helped me out, thanks.

  11. Ciro on June 11th, 2013 9:35 am

    You made my day man, thanks!

  12. m.qayyum on September 17th, 2013 6:59 am

    That is a nice catch. Thanks