System.ExecutionEngineException occurs when displaying WPF window
Article
This article helps you resolve the problem where attempting to instantiate another WPF Window results in an unhandled exception after closing one Windows Presentation Foundation (WPF) Window.
Original product version: Visual Studio Professional 2010 Original KB number: 2691806
Symptom
You have developed a Microsoft .NET 3.5 WPF component and hosting it in a non-WPF based client application, such as a Windows Forms application or native application. The client application calls into the WPF component to instantiate and display a custom WPF Window. The first instantiation of the Windows displays as expected. However, after closing the Window and attempting to instantiate another WPF Window results in an unhandled exception.
If running the application in the Visual Studio IDE, you receive following exception and call stack:
Console
System.ExecutionEngineException: Exception of type 'System.ExecutionEngineException' was thrown.
WindowsBase.dll!MS.Internal.Invariant.FailFast(string message, string detailMessage)
WindowsBase.dll!MS.Internal.Invariant.Assert(bool condition, string invariantMessage)
PresentationFramework.dll!System.Windows.Application.GetResourceOrContentPart(System.Uri uri)
PresentationFramework.dll!System.Windows.Application.LoadComponent(object component, System.Uri resourceLocator)
WPFClassLibrary.dll!WPFClassLibrary.WPFWindow.InitializeComponent()
WPFClassLibrary.dll!WPFClassLibrary.WPFWindow.WPFWindow()
WPFClassLibrary.dll!WPFClassLibrary.WPFManager.ShowWpfWindow()
WindowsFormsApplication1.exe!WindowsFormsApplication1.Form1.button1_Click(object sender, System.EventArgs e)
If running the application under a native debugger, such as Windbg, you receive a breakpoint exception with the following call stack.
eax=00000001 ebx=00000000 ecx=00000001 edx=001becbc esi=79aedfd0 edi=577ac529
eip=76c3280c esp=001be81c ebp=001becc8 iopl=0 nv up ei pl nz na po nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000202
KERNELBASE!DebugBreak+0x2: 76c3280c cc int 3
The crash symptom occurs when the application calls into the System.Windows.Application.LoadComponent method to load a XAML file, associated with a WPF Window or UserControl. Eventually, the WPF run-time calls into System.Windows.Application.GetResourceOrContentPart to load resource packages associated with the current System.Windows.Application object.
This failure occurs because the resource packages associated with the current System.Windows.Application object could not be retrieved. This usually occurs because the current System.Windows.Application object has already been shut down, either by an explicit or implicit call to Application.ShutDown.
This behavior is by design.
Resolution
When creating the instance of the System.Windows.Application object, set its ShutDownMode property to ShutDownMode.OnExplicitShutdown. This will only allow the Application object to be shut down as a result of custom code explicitly calling Application.ShutDown.
More information
When the Application.ShutDown method is called, the WPF run-time unloads the resource packages associated with that Application object. At some point later, if the application does something that requires accessing the resources associated with the same Application object, the System.Windows.Application.GetResourceOrContentPart method attempts to load those resource packages, but is unable to because they were previously unloaded. When WPF detects this, it is considered a fatal condition and WPF purposely calls System.Environment.FailFast, which throws a System.ExecutionEngineException and terminates the process.
A client application is allowed to create one instance of an System.Windows.Application (or derived) type per AppDomain. The resources associated with that Application object are unloaded when the Application.ShutDown method is called. The Application.ShutDown method may be called explicitly by custom code, or it may be called internally by the WPF run-time based on the value of the Application.ShutDownMode property. By default, the ShutDownMode property of the Application object is set to ShutDownMode.OnLastWindowClose. This will cause the WPF run-time to shut down the Application object automatically after the last WPF window associated with that Application object has been closed. In a scenario where a client application has created an instance of a WPF Application object and displayed a Window and closed it, that window is deemed to be the last window closed, so WPF will automatically call Application.ShutDown.
Windows developers have various options for creating applications that run on Windows. This module introduces the native Windows UI frameworks that are available for Windows development. It also provides guidance on how to choose the best framework for your application.