Reputation: 2375
My addin is written in c#, NetOffice, ExcelDNA using WPFframework. Some part uses winforms, too. The main UI is WPF
When there is a modal dialog displayed, users force close Excel. Next time when they launch excel, Excel will say " Excel experienced a serious problem with the '*' add-in. If you have seen this message multiple times, you should disable this add0in and checke to see if an update is available. Do you want to disable this add-in?"
Yes, No
Users usually click Yes or enter without reading the message and then my add-in disappears from Excel. So I do not want this dialog to show up. Is it possible and how? thanks
I try to catch all exception in AutoOpen() like below. But it seems have no effect to stop the dialog at all.
public void AutoOpen()
{
.....
System.Windows.Forms.Application.ThreadException += ApplicationOnThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
....
}
public void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
Helper.LogError(e.Exception);
}
public void ApplicationOnThreadException(object sender, ThreadExceptionEventArgs threadExceptionEventArgs)
{
Helper.LogError(threadExceptionEventArgs.Exception);
}
public void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
if (!(args.ExceptionObject is ThreadAbortException))
{
Exception exc = args.ExceptionObject as Exception;
Helper.LogError(exc);
}
}
public void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Helper.LogError(e.Exception);
e.Handled = true;
}
Upvotes: 3
Views: 2352
Reputation: 16907
I presume by 'users force close Excel' you mean the user ends the Excel process from Task Manager or something.
Excel puts some internal guards in place around ribbon handler calls, so that if Excel crashes during a ribbon event handler, Excel knows which add-in was called when the crash happened, to disable next time as you describe. So if Excel is terminated unexpectedly while your modal dialog is shown, your add-in is the one remembered as the 'cause'.
Your attempt at handling unhandled exceptions is not likely to work, since .NET is hosted in a native process (in Excel). So unhandled exceptions which bubble up to Excel won't be returned to the .NET runtime, but will more likely crash the whole Excel process. So trying to handle more exceptions is not likely to help.
Perhaps a modal dialog is not the right approach, since it causes your users to get confused and think Excel has crashed, causing the unexpected termination. At least be sure to set the Excel window as the parent of the modal dialog, so that the dialog stays in front of Excel.
Upvotes: 3