当Excel强制关闭时,如何禁止禁用加载项对话框 [英] How to suppress Disable add-in dialog when Excel is force close

查看:513
本文介绍了当Excel强制关闭时,如何禁止禁用加载项对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的插件是用c#,NetOffice,ExcelDNA使用WPFframework编写的。一些部分也使用winforms。
主界面是WPF



当显示模态对话框时,用户强制关闭Excel。
下次启动excel时,Excel会说Excel遇到严重问题, * 加载项,如果你多次看到这个消息,你应该禁用这个add0in是否可以使用更新?是否要禁用此加载项?



是,否



用户通常单击是或输入而不阅读消息,然后我的加载项从Excel消失。
所以我不希望这个对话框出现。有可能吗?谢谢



我尝试在AutoOpen()中捕获所有异常,如下所示。但似乎根本没有停止对话的效果。

  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。例外);
}

public void ApplicationOnThreadException(object sender,ThreadExceptionEventArgs threadExceptionEventArgs)
{
Helper.LogError(threadExceptionEventArgs.Exception);


public void CurrentDomainUnhandledException(object sender,UnhandledExceptionEventArgs args)
{
if(!(args.ExceptionObject为ThreadAbortException))
{
异常exc = args.ExceptionObject为异常;
Helper.LogError(exc);
}
}

public void CurrentDispatcher_UnhandledException(object sender,DispatcherUnhandledExceptionEventArgs e)
{
Helper.LogError(e.Exception);
e.Handled = true;
}


解决方案

关闭Excel您的意思是用户从任务管理器或某些东西结束Excel进程。



Excel将一些内部的防护装置放在功能区处理程序调用周围,以便Excel如果崩溃一个功能区事件处理程序,Excel知道在崩溃发生时调用哪个加载项,禁用下一次你所描述的。因此,如果Excel在您的模态对话框显示时意外终止,则您的加载项被记住为原因。



您尝试处理未处理的异常不是可能会工作,因为.NET托管在本机进程中(在Excel中)。所以冒泡到Excel的未处理的异常将不会返回到.NET运行时,但更有可能导致整个Excel进程崩溃。所以尝试处理更多的例外是不太可能的帮助。



也许一个模态对话框不是正确的方法,因为它会导致你的用户困惑,认为Excel已经崩溃,导致意外终止。至少要确保将Excel窗口设置为模态对话框的父级,以使对话框保持在Excel的前面。


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

解决方案

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.

这篇关于当Excel强制关闭时,如何禁止禁用加载项对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆