4 Replies Latest reply on Dec 20, 2012 4:02 PM by drabinowitz

    trying to develop element relative display context change event handler in c# processbook add-in

      Hi I'm trying to develop element relative display context change event handler in c# processbook add-in and I'm running into some trouble.  I tried to set up my context change event handler in the same fashion as kb00425 but I guess I'm not translating things correctly:

       

      this.app.ContextHandlers.Item("E").ContextChanged += new EContextHandlerEvents_ContextChangedEventHandler(Connect_ContextChanged);
      }

       

      public void Connect_ContextChanged(out Display FromDisplay, out ContextHandler FromContextHandler)
      {
      FromDisplay = this.app.ActiveDisplay;
      FromContextHandler = this.app.ActiveDisplay.ContextHandlers.Item("E");
      string tags;
      MessageBox.Show("hello");
      foreach (Symbol groupsym in app.ActiveDisplay.SelectedSymbols)
      {
      tags = groupsym.GetTagName(1);
      MessageBox.Show(tags);
      }
      }

       

      I run the add-in and then open up an element relative display in my processbook and click through the elements but nothing seems to update.  Any suggestions?

       

       

       

      Thanks a lot in advance,

       

      Dmitri Rabinowitz

        • Re: trying to develop element relative display context change event handler in c# processbook add-in
          Asle Frantzen

          Hi Dmitri

           

          Which type of add-in are you creating?

           

          Docking window or dataset addin?

           

          I recently did this for a dataset addin, and couldn't get it to work at first. But then it was just a matter of putting this in the right file, if I remember correctly. Let's see here...

           

           

           

          In the Dataset class (inheriting from IDataProvider3) I do something like this:

           
          public RegressionLineDataset(PBObjLib.Application Application)
                  {
                      this.App = Application;
                      this.App.DisplayActivate += new PBObjLib.EApplication_DisplayActivateEventHandler(App_DisplayActivate);
                      this.App.DisplayDeactivate += new PBObjLib.EApplication_DisplayDeactivateEventHandler(App_DisplayDeactivate);              
                  }
          
          
                  public void App_DisplayActivate(PBObjLib.Display myDisplay)
                  {
                      foreach (PBObjLib.ContextHandler ch in App.ContextHandlers)
                      {
                          if (ch.Name == "E")
                          {
                              m_EContextHandler = ch;
                              m_EContextHandler.ContextChanged += new PBObjLib.EContextHandlerEvents_ContextChangedEventHandler(eContextHandler_ContextChanged);
                          }
                      }                
                  }
          
                  public void App_DisplayDeactivate(PBObjLib.Display myDisplay)
                  {
                      foreach (PBObjLib.ContextHandler ch in App.ContextHandlers)
                      {
                          if (ch.Name == "E")
                          {
                              m_EContextHandler = ch;
                              m_EContextHandler.ContextChanged -= eContextHandler_ContextChanged;
                          }
                      }
                  }
          
                  public void eContextHandler_ContextChanged(out PBObjLib.Display myDisplay, out PBObjLib.ContextHandler myContextHandler)
                  {
                      myContextHandler = this.App.ContextHandlers.Item("E");            
                      myDisplay = this.App.ActiveDisplay;
          
                      CurrentContext = myContextHandler.get_CurrentContext(myDisplay);
                      this.Refresh();
                  }
          

           

          • Re: trying to develop element relative display context change event handler in c# processbook add-in
            dhollebeek

            This may be where C# is getting a little too smart for you.  We have our own way of handling our events internally in our addins, but I think you are running into something we ran into when we originally wrote our addins.  You are assigning the new event handler to this.app.ContextHandlers.Item("E").ContextChanged, but that event is really implemented through COM since the PB code base is not .Net.  This means there really isn't any .Net reference to the new context handler.  There's just a COM references to an interop.  Therefore, when the context handler goes out of scope at the end of the initialization routine, it's .Net reference count is technically zero, so it's vulnerable to being garbage collected away.  I've even seen cases where the event handler works for a few seconds and then stops once it's garbage collected.  The work-around for this is to create a member variable to hold the event handler so that there is always a reference to it.  For example:

             
              m_EHandler = new EContextHandlerEvents...
              this.app.ContextHandlers.Item("E").ContextChanged += m_EHandler;