Search code examples
c#outlookvstooffice-addinsaccess-violation

How do I display a messagebox after an AccessViolationException is thrown


I would like to display a MessageBox whenever a System.AccessViolationException exception is thrown.

We have a VSTO Add-In that encounters an AccessViolationException Exception occasionally.

I've added the attributes [HandleProcessCorruptedStateExceptions] and [SecurityCritical]. The exception is being caught.

Without the exception handler, Outlook restarts and logs the exception to the Application log (Event Viewer).

I would like to log the exception myself and put up a dialog letting users know that the system has become unstable and will need to be restarted manually (I have code to kill the Outlook process)

Throwing up a regular Modal Messagebox just hangs everything. I guess I need to display a dialog in a separate thread; not sure how to do that.

[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
private void myInspector_Activate()
{
            
        
    try
    {
       // Crashing code
          
    }

    catch (System.AccessViolationException ex)
    {
        // write to log

        // THIS NEXT LINE JUST HANGS                     
        System.Windows.Forms.MessageBox.Show("Hello There!");

       // THIS CLOSES OUTLOOK                                
       Process.GetCurrentProcess().Kill();
               
    }
    catch (System.Exception e)
    {
         // Write to Log
    }
}


Solution

  • Inspector.Activate fires on the main Outlook thread, so no reason to switch to the main thread to show a message box.

    You really need to provide the user with the pdb file and log the exception call stack (Exception.ToString() will include it) to figure out where the exception occurs and fix it.