How to pass unhandled exception to application catch block in thread

493 views Asked by At

I currently have an application that opens an ActiveX object that displays information to the user. The COM object needs to be in a new thread in order to be modal. 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.
  2. In an attempt to handle this exception, it is encapsulated in a try/catch block, but is never caught by the catch. Because it is unhandled, I attempted to use Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) which was suggested in multiple posts on SO.
  3. Once the base.Show() is hit, a dialog appears and if continued through it, the program runs without issue.

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.
  3. Open up a window without base.Show() that will allow the ActiveX control to show.

Code for the Show method is as follows:

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.

Thanks in advance for any help!

1

There are 1 answers

0
DiggityCS On

To fix this issue, I was able to change the:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

to

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

this solved the issue.