2 Replies Latest reply on Mar 14, 2012 2:15 PM by Asle Frantzen

    ContextHandler event happens too quick

    Asle Frantzen

      Hi

       

      According to this thread ([DEAD LINK] http://vcampus.osisoft.com/discussion_hall/add_in_microsoft_development/f/11/p/1608/8057.aspx) it seems that the ContextHandler event is early in the context changing process.

       

      I have 3 values which get their values based on the context, and whenever the context handler event kicks in I want to update the traces on a trend. Now the problem seems to be that these value-symbols haven't been updated with their new context-sensitive information, OR that I can't see any changes in the trend after inserting/updating the traces.

       

      Any tips 'n tricks for doing what I want?

       

      It all seems to work if I input a breakpoint or just show a message box with some random stuff early in my UpdateTrend-method.

       

       

       

      Simplified code:

       

       

       
      Private WithEvents myCtxtHndlr As ContextHandler
      
      Private Sub Display_Open()
          Set myCtxtHndlr = Application.ContextHandlers("E")
      End Sub
      
      Private Sub myCtxtHndlr_ContextChanged(FromDisplay As Display, FromContextHandler As ContextHandler)
          UpdateTrend thisTrend:=Trend1
      End Sub
      
      Private Sub UpdateTrend(ByRef thisTrend As Trend)
          
          Dim vIndex As Integer
          Dim status As Boolean
          Dim restatus As Variant
          Dim trafo1 As String    
          Dim trace1 As String
          
          'Remove all trend traces
          For vIndex = thisTrend.TraceCount To 1 Step -1
              status = thisTrend.RemoveTrace(vIndex)
          Next vIndex
          
          'Check and set trace
          trafo1 = value_transformer1.GetValue(Now, restatus)
          If (trafo1 <> "n" And trafo1 <> "") Then
              trace1 = "E.\Equipment repository\Transformers\" & trafo1 & "|HV Active power"
              status = thisTrend.AddTrace(trace1)
          End If
      
      End Sub
      

       

        • Re: ContextHandler event happens too quick
          andreas

          This sounds similar (the workaround) as what I saw here; vcampus.osisoft.com/.../2599.aspx

           

          Could you check if an ThisDisplay.Activate similar to the MessageBox helps?

            • Re: ContextHandler event happens too quick
              Asle Frantzen

              It did sound similar, I didn't find that thread yesterday while searching.

               

              But I ended up doing what that link I referred to said, using the OnIdle() sub. So I'm just setting a boolean value (allowedToUpdate=True) in the context handler now, and in the OnIdle sub I'll do the trend updating if allowedToUpdate=True.

               

              The thing I found a bit strange with the ContextHandler is that I would expect it to run after the context had changed - and all the context sensitive values in the display had been updated. I would think that it would be more useful. In reality, this should really be called BeforeContextChanged(…) the same way you have the other Before- and After- events for Display.

               

              Or at least a new AfterContextChanged(...) should be added.

               

              Thanks!