Blog Indiana 2008

me.ShowDialog() and LoaderLock

I like to keep a good separation between forms and function in Windows applications I write. So to call a login form, I might call a public function on the login form like: Login.GetLogin(loginName, password).

GetLogin might look like this (VB.NET):


Public Function GetLogin(ByVal strLogin As String, ByVal strPassword As String) As Integer
   txtLogin.Text = strLogin.Trim
   txtPassword.Text = strPassword.Trim
   Me.ShowDialog()
   Return mintToken
End Function

In .NET 2.0 and Visual Studio 2005, this causes an exception to pop-up:

LoaderLock was detected

Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

This exception is thrown on the first keystroke of my login form.

This seems to be a common problem, and the solution is to shut off the LoaderLock MDA (Managed Debugging Assistant). This can be done in the menu Debug -> Exceptions -> Managed Debug Assistants and unchecking the LoaderLock option. I don’t like this solution, but it seems to work.

This Microsoft KB Article on LoaderLock contains some good information about this issue. One of the commenters stated they had the same problem with an application throwing this each time a key is pressed, but no one has replied with a reason why. My Visual Studio 2005 project contains a form originally designed in VS 2003. I don’t know if that has something to do with it or not.

Hat tip: chrisB. Also Simon’s Software Stuffp

Posted on March 28, 2007 in .NET.    

3 Comments

  1. Gaco said:

    I’ve been researching the same issue, of having iTunes and VS2005 not working toghether. And as you mention, it seems like most people are just turning off that option in VS, which I feel is the wrong answer.
    Granted, it will let you move on with your project while you happily listen to iTunes, but it will hide the problem, if it ever appears for real in your app.
    anyway, I’m glad you were able to find a (temporary) solution for your problem.

  2. CoDE said:

    Been looking around, it seems that this error is related to iTunes, believe it or not!

  3. John said:

    I agree, and this is not the optimal solution. However, it is the one we’ll need to deal with since neither party seems interested in working on getting it resolved.

(Comments are moderated.)