Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
PRB: Visual Studio .NET events being disconnected from add-in.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: August 2005   Microsoft Visual Studio .NET 2003
      Microsoft Visual Studio 2005
Introduction

This article describes why after some time a Visual Studio .NET add-in no longer receives events.

More Information

When using a C# add-in to receive events from Visual Studio .NET, it can happen that after a while the add-in no longer receives them. Consider this code:

private _DTE applicationObject;
 
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, 
   object addInInst, ref System.Array custom)
{
   EnvDTE.SolutionEvents solutionEvents;
   
   applicationObject = (_DTE) application;
   solutionEvents = applicationObject.Events.SolutionEvents;
   solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(SolutionOpened);
}
 
private void SolutionOpened()
{
   MessageBox.Show("Solution opened");
}
When you call Events.SolutionEvents, a .NET wrapper object is created around the underlying COM object that Visual Studio (which is a COM application, not a .NET application) holds to raise the events. However, Visual Studio doesn't hold a reference to this .NET wrapper, which is only referenced by the solutionEvents variable. Since this variable is local to the OnConnection procedure, when the procedure exits the variable is no longer used and it will be garbage collected the next time that the garbage collector performs its job. Until that moment you receive events, but after that the event handler is disconnected. To solve this problem, you must declare the solutionEvents variable at class level, not at procedure level:
private _DTE applicationObject;
private EnvDTE.SolutionEvents solutionEvents;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, 
   object addInInst, ref System.Array custom)
{
   applicationObject = (_DTE) application;
   solutionEvents = applicationObject.Events.SolutionEvents;
   solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(SolutionOpened);
}
 
private void SolutionOpened()
{
   MessageBox.Show("Solution opened");
}

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top