none
Addin: DocumentEvents are not triggered

    Question

  • I have written an add in that takes the active document as a parameter. So each time the active document has changed, I need to know. To do so, I wanted to use "Events.DocumentEvents.DocumentOpened" event of the DTE2 object. But the problem is that event is never get fired even though I change the active document.

    I use Visual Studio 2010. I have tried with other DocumentEvents as well but no success. Any ideas?

    The code snippet is as follows

    private DTE2 _applicationObject;
    
    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    		{
    			_applicationObject = (DTE2)application;
    
          _applicationObject.Events.DocumentEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(DocumentEvents_DocumentOpened);
          _applicationObject.Events.DocumentEvents.DocumentClosing += new _dispDocumentEvents_DocumentClosingEventHandler(DocumentEvents_DocumentClosing);
          _applicationObject.Events.DocumentEvents.DocumentOpening += new _dispDocumentEvents_DocumentOpeningEventHandler(DocumentEvents_DocumentOpening);
          _applicationObject.Events.DocumentEvents.DocumentSaved += new _dispDocumentEvents_DocumentSavedEventHandler(DocumentEvents_DocumentSaved);
    
            ....
    
            }
    
     void DocumentEvents_DocumentOpened(Document Document)
        {
          MessageBox.Show("Never called");
        }
    

    Wednesday, March 2, 2011 8:44 AM

Answers

All replies

  • You need to save a strong reference to both the Events and DocumentEvents objects or else they will get GC'ed.

    Ryan

    Wednesday, March 2, 2011 3:36 PM
    Moderator
  • I am not familiar with weak and strong references but as far as I understand I save the reference to a weak reference and use the target property of this weak reference whenever I need the strong reference. But in that case when do I need, just before event definition? Can you please describe me how to save a strong reference in my case so they are not GC'ed.

    I modified my code to this, but I'm not sure what I am doing;

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    		{
    	  _applicationObject = (DTE2)application;
    
          WeakReference applicationObjectWeakRef = new WeakReference(application);
    
          _applicationObject = applicationObjectWeakRef.Target as DTE2;
    
          _applicationObject.Events.DocumentEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(DocumentEvents_DocumentOpened);
    
          ...
    

    Thursday, March 3, 2011 11:11 AM
  • I had just realized that I focused on the wrong event and thats why it was not fired. With the code below I got what I intended to. So instead of DocumentEvents, I had to use WindowEvents.

       ....      
     
      _applicationObject.Events.WindowEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WindowEvents_WindowActivated);
    
      }
    
      void WindowEvents_WindowActivated(Window GotFocus, Window LostFocus)
      {
        if (ucCAST != null && GotFocus.Document != null)
          ((CAST)ucCAST).refreshCode(GotFocus.Document.Name);
      }

    Thursday, March 3, 2011 2:28 PM
  • By strong reference I meant a class field of your AddIn class, what you have above won't help as WeakReference doesn't prevent garbage collection.  It appears from your other response that you were using the wrong events, though even with those other events if you don't keep a strong reference to the Events object in your AddIn class beyond your OnConnection method it will be garbage collected and you will simply stop receiving events at some (non-deterministic) point in time.

    Ryan

    Thursday, March 3, 2011 3:58 PM
    Moderator