MissingMethodException and WaitOne

2008-09-26
A customer complained today about a WinForms application written for .NET 2.0 that crashed. It turns out that on the customer's computer, a MissingMethodException is thrown when calling:
bool signal = WaitHandle.WaitOne(0);
Well, it works fine on my computer and I was certain that the project settings in Visual Studio 2008 was set to .NET version 2.0 and that the customer has that version of .NET installed. It is a system method, so it should be there, shouldn't it?

Not so. Turns out, if I’m not mistaken here, that this a new overload of the WaitOne method that was introduced in .NET 2.0 SP2. There are several overloads of this method, and the two I was having trouble choosing between was:

bool WaitOne(int millisecondsTimeout, bool exitContext);
bool WaitOne(int millisecondsTimeout);

I must confess that I’m not entirely sure what exitContext means and the difference in supplying true or false. So out of laziness, when Visual Studio presented me with these choices, I chose the latter without the bool argument. Bad choice!

From what I understand this overload was actually added in .NET Framework 2.0 Service Pack 2. However it seems that SP2 is not installed by Windows Update and isn’t even available as a separate download. It is only available as part of .NET Framework 3.5 Service Pack 1.

You might know about the confusion about .NET Framework versions that seems to have replaced the old COM based dll hell. I’m probably not the best person to explain it and I sure use my own terminology. However, I’m going to give it a shot:

The thing is that one differentiates between the “core” .NET and “extensions” to .NET. Since version 3.0 of .NET no new versions has been created for the “core”. All libraries from version 3.0 and up are “extensions” and they all depend on the “core”. The “core” .NET is therefore still in version 2.0. However, I guess some bugs might have been discovered but also I’m guessing that while building the “extensions” a need for some updates in the “core” arised, which is why the “core” still had to be changed. And this lead to the release of service packs for the “core”. The result is that the package known as “.NET 3.5 SP1″ includes an update to the core known as “.NET 2.0 SP2″.

Anyway, there are two “fixes” for my problem:

I chose the first one.

To Visual Studio, one might request that you had warned me about this. There is a generation of warnings when building applications where you use certain classes and methods that “should not be used anymore”. I guess this is the opposite, but should still warrant a warning?

Note: Please make a comment if you have a better understanding of this issue than I have. I can’t say I have verified all my findings.

21 Responses to “MissingMethodException and WaitOne”

  1. Vaclav Kabat says:

    my primary issue is that both dlls (before and after) have same version

  2. joe says:

    these new overloads stung me and I wasted many hours. they shouldn’t introduce new overloads in a service pack, if you ask me.

  3. Michael says:

    Mike, thanks for the info! You helped me a lot.

  4. listar says:

    Mike, thanks! I got a few hours of headache with MissingMethodException and finally I found your article! Thanks again!!! You save my day =)

  5. Luckyrat says:

    +1

    Took me a while to realise this was my problem (I am calling the method via a separate unmanaged process and with my lack of C++ expertise it was ages before I even considered it could be a .NET failure).

    Thanks!

  6. Chris says:

    +1

    Wow, I spent a few hours debugging this problem, the customer had .Net framework 3.5 (not SP1) installed, it worked fine for me but not for him… I think google suggest was the biggest help, I was searching for “missingmethodexception” and it suggested that I add “waitone” to the search string, that’s how I found this article :)

    Thanks!!!

  7. Martin says:

    Thanks Mike, this saved me a lot of time.

    It seems to me that the “multi-targeting” feature of VS2008 is somewhat broken. I ask it to target v2.0 of the .NET framework, and it allows me to write code that only works in a version of v2.0 that’s deployed with v3.5.

  8. Peter Stephens says:

    One note: the documentation for WaitHandle.WaitOne(int) claims that it will work for .NET 2.0 SP1. This is not true as you have found out. 2.0 SP2 is required.

  9. [...] .NET Development — dfbaskin @ 12:59 pm I ran into the same problem as Mike Plate describes here. Fortunately, there is a now separate download for the .NET 2.0 Service Pack 2 here. Installing [...]

  10. DJacobs says:

    FWIW, I had a P9 system.missingmethodexception error that was crashing a simple application on some machines but not on others. I tracked it down to a “.WaitOne(2000)” line that was executing inside a thread. Never causes a problem on my machine, but it sure crashed on those other machines, EVEN THOUGH THE CODE WAS NEVER EXECUTED! The thread hadn’t even gotten to that line. The crash was occurring when the program started up, which was really frustrating. I even had a “try..catch” around the problem line of code, and it didn’t help. I stopped using the AutoResetEvent and used a variable and a “while( ! signaled ) { Thread.sleep(20); }, which was a kludge, but it worked….

  11. DJacobs says:

    Apparently, this error is thrown by the JIT compiler, which is why no amount of “try..catch” lines will prevent it: http://stackoverflow.com/questions/809337/net-wpf-missingmethodexception-when-launching-new-thread

  12. DavidB says:

    Another vote of thanks.

    I wasn’t even getting the ‘Method Not Found’ error until I put some error handling in the thread constructor:

    Thread myThread = new Thread(() =>
    {
    try
    {
    MyWaitOneMethod();
    }
    catch (Exception ex)
    {
    // Error logging code
    }
    });

  13. CupcakeThief says:

    Nice one, saved me a few hours of headscratching! Not impressed with this one…

  14. Naveen Joshi says:

    Thanks, it saved me!

  15. Peter Goodwine says:

    Three years old and the info is still valuable! Thanks!

  16. Anonymous says:

    Thanks, saved alot of my time.

  17. Any says:

    worked for me as well!

  18. Alexei says:

    Looks like the lesson wasn’t learned:
    .NET Framework 3.5 SP1 ships a new WindowsBase.dll of same v3.0.0.0 with two more overloads of System.Windows.Threading.Dispatcher.BeginInvoke. Sure enough this leads to the above-described failure and no way to leverage the (nice-when-correctly-used) .NET versioning functionality for preventing it. Perhaps, it merits a footnote in the post above.

    > Vaclav Kabat says:
    > my primary issue is that both dlls (before and after) have same version
    Exactly! There are entire treatises [1,2] written on the subject of when to increment version numbers, but I haven’t read anyone disagreeing that at least the minor version number must be bumped when the public interface is extended (otherwise how could one explicitly depend on the newer version?).

    > Martin says:
    > It seems to me that the “multi-targeting” feature of VS2008 is somewhat broken. I ask it to target v2.0 of the .NET framework, and it allows me to write code that only works in a version of v2.0 that’s deployed with v3.5.
    Exactly! Same holds for targeting to 3.5 and having the build work only on 3.5 SP1.

  19. Ingo says:

    Many thanks, i lost hours of time last night. Same executeable works great on my development computer (Win7 VS2010/incl. Frameworks 1.1 to 4.0). But the missingmethod exception comes up on the target platform winxp embedded with Framework 2.0 only.

  20. Bob says:

    Adding another onto the “you just saved me hours and hours of work” pile, thanks so much for this.

  21. Leonardo Brogelli says:

    They are crazy! I’ve lost a lot of hours to debug an application for the same reason.
    WaitOne(timeout) -> will works on some machine and crashes on some others.
    WaitOne(timeout, false) -> works !!!

    Thank You MS

Leave a Reply

Twitter: @mikeplate