Kello’s Code Corner

August 17, 2009

ExecutionEngineException when trying to open WPF Window a second time in a WinForms application

Filed under: .NET,C#,VB.NET,WPF — jonaskello @ 19:34

The problem:

Today when I was coding away on an application which is based on WinForms but contains WPF UserControls I had to add a WPF Window to show a modal dialog. This worked fine until I tried to open the WPF window, close it and then open it again. The second time I tired to open the WPF window I ran into a System.ExecutionEngineException.

I found this very strange at first but it is actually totally logical! It all became clear to me when I found this post. Since I am using both WinForms and WPF there are two Application objects (I blogged about creating the WPF application object in a WinForms application to use application level WPF resources). The problem is that the WPF application object that is created has a defult setting of ShutdownMode = OnLastWindowClose. So what happens is:

  1. The WinForms application is started and creates an WPF application object to enable application level resources.
  2. A WPF window is opened. This is now the first and only WPF window running in the context of the WPF Application object.
  3. The WPF window is closed, this causes the WPF Application object to execute its Shutdown method because of its default setting of ShutdownMode = OnLastWindowClose.
  4. An attempt is made to open a second WPF window but since the WPF Application has been shutdown the result is a System.ExecutionEngineException.

The solution:

It is very simple to solve this actually. Using the code I presented in the aformentioned post I made the following modifications:

  Sub Main()

    'This will create the WPF Application object but it will only
    'be used for WPF application-level resources since the
    'real application uses the WinForms Application object
    If System.Windows.Application.Current Is Nothing Then
      Dim wpfApp As New WpfControlLibrary1.App
      'Use OnExplicitShutdown instead of the default OnLastWindowClose
      'because otherwise WPF will shutdown when a WPF window is opened
      'and then closed and we will not be able to open it a second time
      wpfApp.ShutdownMode = ShutdownMode.OnExplicitShutdown
    End If

    Application.Run(Form1)

    'Now that the WinForms application is done we explicitly shutdown the WPF application
    If System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown Then
      System.Windows.Application.Current.Shutdown()
    End If

  End Sub
Advertisements

3 Comments »

  1. Jonas,
    Thanks for posting that: you just saved me a lot of time!

    Sam

    Comment by Samuel Jack — September 30, 2009 @ 16:08 | Reply

    • I should add (just in case anybody is googling this) that in my case I’m using WPF inside an VSTO Excel Addin (actually an Addin-Express addin, but it’s pretty much the same thing)

      Comment by Samuel Jack — September 30, 2009 @ 16:16 | Reply

  2. Hi,
    I am creating a vb.net application. I want to create the login window in WPF and use from the vb.net application. How can i achieve this

    Comment by Bijesh — November 26, 2010 @ 19:02 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: