简体   繁体   English

如何将未处理的异常传递给线程中的应用程序捕获块

[英]How to pass unhandled exception to application catch block in thread

I currently have an application that opens an ActiveX object that displays information to the user. 我目前有一个打开可向用户显示信息的ActiveX对象的应用程序。 The COM object needs to be in a new thread in order to be modal. COM对象需要处于新线程中才能成为模式对象。 There are currently a couple of issues I am having with it: 目前,我遇到了几个问题:

  1. I use a base.Show() method that is used to display the ActiveX object, and it throws an InvalidOperationException during runtime which is shown through VisualStudio by throwing Common Language Runtime Exceptions. 我使用用于显示ActiveX对象的base.Show()方法,它在运行时引发InvalidOperationException,该异常通过VisualStudio通过引发公共语言运行时异常来显示。
  2. In an attempt to handle this exception, it is encapsulated in a try/catch block, but is never caught by the catch. 为了尝试处理此异常,将其封装在try / catch块中,但永远不会被catch捕获。 Because it is unhandled, I attempted to use Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) which was suggested in multiple posts on SO. 因为未处理,所以我尝试使用Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException),这在SO的多篇文章中都有建议。
  3. Once the base.Show() is hit, a dialog appears and if continued through it, the program runs without issue. 命中base.Show()后,将出现一个对话框,如果继续进行该对话框,则程序将正常运行。

A couple of solutions could be: 一些解决方案可能是:

  1. Disable the dialog box that shows that the exception is unhandled. 禁用显示未处理异常的对话框。
  2. When the exception is thrown, log it, but still allow the base.Show() to execute. 引发异常时,将其记录下来,但仍允许执行base.Show()。
  3. Open up a window without base.Show() that will allow the ActiveX control to show. 打开一个没有base.Show()的窗口,该窗口将允许ActiveX控件显示。

Code for the Show method is as follows: Show方法的代码如下:

public void Show(string url, int entityId, string sessionId, int projId, string docId)
{
    try
    {
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        base.Show(); //exception occurs here
    }
    catch (Exception ex)
    {
        //continue without dialog because once continuing in the dialog, application runs without error
        Logger.Error("Base.Show() throws InvalidOperationException, but continuing will bypass issue", ex);
    }

    try
    {
        this.DocViewer.InitComm(url, entityId, sessionId, projId, docId);
    }
    catch (Exception ex)
    {
        Logger.Error("Error opening papervision viewer", ex);
        throw;
    }
}    

I am unsure how to access this method in the main thread, and have attempted to use BeginInvoke, but the base.Show() must open the window before the DocViewer.InitComm is run. 我不确定如何在主线程中访问此方法,并且已经尝试使用BeginInvoke,但是base.Show()必须在运行DocViewer.InitComm之前打开窗口。

Thanks in advance for any help! 在此先感谢您的帮助!

To fix this issue, I was able to change the: 为了解决这个问题,我可以更改:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

to

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

this solved the issue. 这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM