Logo
BUG: Visual Studio 2008 crashes if unloaded when an add-in executes DoEvents statement on startup

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2008
Date: June 2015  
     
     
Introduction

If an add-in executes the System.Windows.Forms.Application.DoEvents() statement during its initialization, Visual Studio 2008 crashes if it is unloaded quickly either by hand or by an external script. An add-in may want to execute the DoEvents() statement to show its toolbars immediately after creation while other initialization code remains to be executed. Visual Studio may close quickly after launched if the following script is executed to reset add-ins:

devenv.exe /resetaddin * /Command File.Exit

More Information

This is a bug that affects only to Visual Studio 2008. Older and newer versions don't exhibit this bug. The steps to reproduce the problem are the following:

  • Create an add-in with this code using Visual Studio 2008 and build it:
Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;

namespace MyAddin
{
   public class Connect : IDTExtensibility2
   {

      private DTE2 _applicationObject;
      private AddIn _addInInstance;
      
      public void OnConnection(object application, ext_ConnectMode connectMode, 
         object addInInst, ref Array custom)
      {
         _applicationObject = (DTE2)application;
         _addInInstance = (AddIn)addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_AfterStartup:

               InitializeAddIn();
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // Do nothing. OnStartupComplete will be called
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         System.Windows.Forms.Application.DoEvents();
      }
      
      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }


      public void OnBeginShutdown(ref Array custom)
      {
      }
   }
}
 
  • Open a Visual Studio 2008 Command Prompt and execute this statement:

    devenv.exe /Command File.Exit
  • You get a crash.


Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)