How to suppress Disable add-in dialog when Excel is force close

帅比萌擦擦* 提交于 2019-12-10 20:33:53

问题


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;
    }

回答1:


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.



来源:https://stackoverflow.com/questions/16447595/how-to-suppress-disable-add-in-dialog-when-excel-is-force-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!