How to Detect if Visual Studio IDE is closing using VSPackage?

这一生的挚爱 提交于 2019-12-04 10:57:51

Just to make it explicit and close this problem. This is the snapshot of code to check if VS is closing:

// Create 2 variables below
private DTE2 m_applicationObject = null;
DTEEvents m_packageDTEEvents = null;

Then in Initialize add this:

// Link the Event when VS CLOSING                
m_packageDTEEvents = ApplicationObject.Events.DTEEvents;
m_packageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutDown);

Two other methods that you need:

 public DTE2 ApplicationObject
 {
        get
        {
            if (m_applicationObject == null)
            {
                // Get an instance of the currently running Visual Studio IDE
                DTE dte = (DTE)GetService(typeof(DTE));
                m_applicationObject = dte as DTE2;
            }
            return m_applicationObject;
        }
  }

And

public void HandleVisualStudioShutDown()
{
    MessageBox.Show("Exiting Visual Studio. Bye");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!