Christo S. Christov
Christo S. Christov

Reputation: 2309

Application object is being shut down exception

I'm developing an Excel add-in using multiple windows forms and multiple Element host objects to contain WPF controls. I also have a form which calls to a web service. And displays the results back to Excel.

The problem I'm currently facing is the current : After some interaction with the addin ( no specific order has been found ) my addin stops behaving correctly, throwing an invalid operation exception somewhere inside the PresentationFramework.dll . I can't inspect the code that's running in there because it's hidden.

The Exception says the following : "Application object is being shut down".

What could be causing this?

It is thrown at initialization of a control that is used for some custom drawings

        InitializeComponent();

The stack trace looks like this :

at System.Windows.Application.GetResourcePackage(Uri packageUri)\r\n at System.Windows.Application.GetResourceOrContentPart(Uri uri)\r\n at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)\r\n at Drawing.MoleculeView.InitializeComponent() in d:\Projects\Common\Depict\Drawing\MoleculeView.xaml:line 1\r\n at Drawing.MoleculeView..ctor() in d:\Projects\Common\Depict\Drawing\MoleculeView.xaml.cs:line 192\r\n at DrawingControlWrapper.MultipleDrawingControl.LoadMolecule(Molecule molecules, Point[] bounds) in c:\Users\Nikolay\Desktop\Addin\DrawingControlWrapper\MultipleDrawingControl.xaml.cs:line 32\r\n at CallSite.Target(Closure , CallSite , MultipleDrawingControl , Object , Point[] )\r\n at LMCExcelFunctions.ThisAddIn.QueueMolecule(Object mol) in c:\Users\Nikolay\Desktop\Addin\LMCExcelFunctions\ThisAddIn.cs:line 182\r\n at CallSite.Target(Closure , CallSite , Type , Object )\r\n at LMCExcelFunctions.ThisAddIn.addin_SheetSelectionChange(Object sh, Range target) in c:\Users\Nikolay\Desktop\Addin\LMCExcelFunctions\ThisAddIn.cs:line 93

XAML looks like this :

<UserControl x:Class="Drawing.MoleculeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel x:Name="layout"   HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    </DockPanel>
</UserControl>

Upvotes: 6

Views: 5811

Answers (4)

In got this exception when running an automated test checking that the service initialization code running just before showing the main window of a WPF app succeeds:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Code to init some services...

    if (isTestRun)
    {
        // Service init code succeeds, quit the app without showing the main window.
        Shutdown(exitCode: 0); // -> This line throws the exception.
    }
    else
    {
        var mainWindow = new MainWindow();
        mainWindow.Show();
    }
}

Replacing the offending line with

Current.Dispatcher.InvokeShutdown();

resolved the issue.

Please note that:

  1. The code above belongs a class deriving from System.Windows.Application.
  2. The exact problem was that the offending line caused the automated tests running with STA attribute to fail.

Upvotes: 0

Ahmed_mag
Ahmed_mag

Reputation: 270

Wpf controls need an instance of application but but winForm doesn't instantiate the wpf application. you can resolve it by the following code at application startup:

        System.Windows.Application app = new System.Windows.Application()
    {
        ShutdownMode = Windows.ShutdownMode.OnExplicitShutdown
    };
    Closed += () =>
    {
        app.Shutdown();
    };

or in Vb.net:

Dim app As System.Windows.Application = New System.Windows.Application With {
.ShutdownMode = Windows.ShutdownMode.OnExplicitShutdown
AddHandler Closed, Sub()
                               app.Shutdown()
                           End Sub

Upvotes: 3

Geoff Scott
Geoff Scott

Reputation: 957

If you set Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown you problem should go away.

Upvotes: 33

Christo S. Christov
Christo S. Christov

Reputation: 2309

Resolved by using an inner Element Host : it appears that any control that is being created by a control which is hosted inside an element host must also have its own element host.

Upvotes: 1

Related Questions