Hello, I am writing a WinForms application in C#.
At one point in the application, I spawn a new STA thread (create thread, then SetApartmentState
) that creates a new form and then shows it with plain old Show()
. The form itself contains only a docked DataGrid
whose DataSource
points to a DataTable
retrieved from a newly open SqlConnection
. All data and UI objects are created on the same thread.
When I execute that code I get a LoaderLock
exception with the following (helpful) text:
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.
I'm doing no such thing, at least not intentionally! The stack trace has only unmanaged code at the head and I'm unable to debug that with C# Express 2008.
Looking around the Internets, people usually just disable that exception within VisualStudio. Is this another case of Microsoft being padantic, or should I actually spend time figuring out what's going on?
Update The error seemed to be caused by opening multiple forms (each with data grid, etc.) rapidly. If I switch the Show()
to ShowDialog()
and view the forms one at a time, then the error goes away.