Revit 插件中的 AppDomain.CurrentDomain.UnhandledException [英] AppDomain.CurrentDomain.UnhandledException in Revit Addin

查看:27
本文介绍了Revit 插件中的 AppDomain.CurrentDomain.UnhandledException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自己的 Revit 插件 中使用崩溃报告器,但从未调用 AppDomain.CurrentDomain.UnhandledException.似乎 Revit 本身管理未处理的期望并显示自己的崩溃对话框.在 Revit 捕获它们之前,我应该怎么做才能在 Revit 插件中捕获所有未处理的异常?

I wanted to use a crash reporter in my own Revit addin but AppDomain.CurrentDomain.UnhandledException is never called. It seems Revit itself manages the unhandled expections and shows its own crash dialog. What should I do to catch all unhandled exceptions in revit addin before Revit cathes them?

我已经尝试了以下代码行,但它不起作用:它从不进入处理程序方法:

I already tried the following lines of code but it does not work: it never enters the handler method:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        throw new NotImplementedException();
    }

感谢您的帮助

推荐答案

我认为在 Revit 主线程中没有办法做到这一点.

I think there is no way to do this in Revit main thread.

但是,如果您将在单独的线程中运行您的插件,Revit 将不会处理该线程中的异常,并且会触发 UnhandledExceptions 事件(很可能 Revit 也会崩溃).

But if you will run your addin in separate thread exceptions from that thread will not be handled by Revit, and UnhandledExceptions event will be fired (most likely Revit will crash as well).

如果您的插件在 wpf 上,您可以像这样在单独的线程中启动它,甚至可以处理来自该线程的异常.

If your addin is on wpf you can launch it in separate thread like this, and even handle exceptions from that thread.

namespace ClassLibrary2
{
    [Transaction(TransactionMode.Manual)]
    public class Startup : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                App.ThisApp.ShowFormSeparateThread();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return Result.Succeeded;
        }
    }
}

_

    public void ShowFormSeparateThread()
    {
        if ( !(_uiThread is null) && _uiThread.IsAlive )
            return;

        _uiThread = new Thread(() =>
        {
            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
            
            Dispatcher.CurrentDispatcher.UnhandledException += (sender, args) =>
            {
                MessageBox.Show("Err" + args.Exception.Message);
                args.Handled = true;
            };
            _mMyForm = new MainWindow();
            _mMyForm.DataContext = new MainViewModel();
            _mMyForm.Closed += (s, e) => Dispatcher.CurrentDispatcher.InvokeShutdown();
            _mMyForm.Show();
            Dispatcher.Run();
        });
        
        _uiThread.SetApartmentState(ApartmentState.STA);
        _uiThread.IsBackground = true;
        
        _uiThread.Start();
        
    }

如果您想获取有关 Revit 中所有异常的信息,即使是其他插件抛出的异常,只需使用

If you want you can get information about ALL exception in Revit even thrown by someone else addins, just use

 AppDomain.CurrentDomain.FirstChanceException += (sender, args) =>
 {
     //your code here
 }; 

这篇关于Revit 插件中的 AppDomain.CurrentDomain.UnhandledException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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