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

1

5

I'm writing a VS Package where I need to store the time when the user start my package, and when the user close the Visual Studio.

The problem is, I don't know how to get the closing event for the Visual Studio. Can anyone give me any information about how to detect if the VS is closing?

Note: When I search from the internet, I got the following similar problem to mine: How do you cancel a ToolWindowPane or Visual Studio IDE close operation via a VSPackage?, but when I try it, this solution is to detect and do something when the Package window is closed, and cannot detecting when the Visual Studio is closed.

Any help is really appreciated.

Thanks

Fontaine answered 4/2, 2013 at 1:11 Comment(3)
Did you try DTEEvents.OnBeginShutdown?Aperient
It works. Thanks. For other, if you want to know the answer, it is in the link that I put above. It seems that I just concentrate more on the first example in the link above. For capturing VS event when it is closing, it is in the third example. Thanks Hans. <BTW, how can I make this thread as solved?>Fontaine
What I mean by the "third example" is the "Wire up DTE Events" example.Fontaine
F
7

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");
}
Fontaine answered 5/2, 2013 at 6:38 Comment(2)
The solution found here https://mcmap.net/q/2035398/-how-do-you-cancel-a-toolwindowpane-or-visual-studio-ide-close-operation-via-a-vspackage is much simpler.Talesman
any reason for complicating the DTE query that much ?Phytogeography

© 2022 - 2024 — McMap. All rights reserved.