Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application object is being shut down exception

Tags:

c#

excel

wpf

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>
like image 528
Christo S. Christov Avatar asked Feb 25 '15 16:02

Christo S. Christov


3 Answers

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

like image 173
Geoff Scott Avatar answered Oct 22 '22 01:10

Geoff Scott


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
like image 3
Ahmed_mag Avatar answered Oct 22 '22 01:10

Ahmed_mag


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.

like image 1
Christo S. Christov Avatar answered Oct 22 '22 00:10

Christo S. Christov