Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Confused: object life cycle

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Confused: object life cycle

    In my continuing education about NT8, I was taking another look at the documentation about ChartControl, and came across the example in https://ninjatrader.com/support/help...collection.htm

    In light of the information at http://ninjatrader.com/support/helpG...fecycle_of.htm, would the buttons being handled in the former link, not get prematurely nulled/destroyed?

    #2
    Hi koganam,

    In the life cycle of the indicator being run and then removed, the termination will only occur twice for that instance. However, NT makes several instances of a script (when opening windows, enabling, removing, and other actions) and you will see the terminated state be hit when an indicator is running, but it will be for a different instance of that script.

    The running instance would be step 3 (the whole run through) in the flow chart of the life cycle documentation. The other steps are different instances of the script.

    In practice, are you finding that buttons added to the UserControlCollection (or added to the WPF elements of the chart) or handlers are being removed prematurely?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi koganam,

      In the life cycle of the indicator being run and then removed, the termination will only occur twice for that instance. However, NT makes several instances of a script (when opening windows, enabling, removing, and other actions) and you will see the terminated state be hit when an indicator is running, but it will be for a different instance of that script.

      The running instance would be step 3 (the whole run through) in the flow chart of the life cycle documentation. The other steps are different instances of the script.

      In practice, are you finding that buttons added to the UserControlCollection (or added to the WPF elements of the chart) or handlers are being removed prematurely?
      I have never tested that particular code, but given that that page ultimately is the explanation for what was happening in this rather long thread: http://ninjatrader.com/support/forum...ate.Terminated, and that the part quoted below, of the explanation of object-life cycle, is required to ensure that the correct instance is the only one affected when the myriad calls to State.Terminated are made, I am asking, because I do not see the identifying flag in the code.

      Attached Files
      Last edited by koganam; 03-07-2017, 04:07 PM.

      Comment


        #4
        Hi koganam,

        This would be good example of this.

        The reason we would check that the toolbar was actually added is because, OnTermination() will trigger twice even though the script was not actually run. In steps 1 and 2 of the life cycle, OnTermination will get hit even though the script never ran. Because of this, any objects that would have been created when the script ran never get created. This could lead to errors if there are null objects, or if the script makes changes to the WPF elements of the chart.

        I've run into this exact issue when making a ChartSidePanelExample which I have attached. OnTermination gets called even though the script wasn't run and was causing the wrong columns to be removed. To fix it, I ensure the extra panel area was actually added first using a bool. (I don't do this in the same place, but its the same concept)
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi koganam,

          This would be good example of this.

          The reason we would check that the toolbar was actually added is because, OnTermination() will trigger twice even though the script was not actually run. In steps 1 and 2 of the life cycle, OnTermination will get hit even though the script never ran. Because of this, any objects that would have been created when the script ran never get created. This could lead to errors if there are null objects, or if the script makes changes to the WPF elements of the chart.

          I've run into this exact issue when making a ChartSidePanelExample which I have attached. OnTermination gets called even though the script wasn't run and was causing the wrong columns to be removed. To fix it, I ensure the extra panel area was actually added first using a bool. (I don't do this in the same place, but its the same concept)
          We must be talking at cross-purposes. I am asking only one question. Given your own demonstration, and the "object life cycle" page that I quoted, "Why would the same problem that you demonstrate in your last post, not happen in the example quoted in the UserControlCollection page?"

          I am not asking for another explanation of how the object life cycle is handled, or other examples of needing to use an isolating flag: I want to know whether the problem described would not mean that the example on the UserControlCollection page is incorrect, and if not, why not?

          Kindly focus on the question. Why is the example code not defective, given how object life cycles are handled means that there will be many calls to State.Terminated that are not called from the object that holds that demonstration code? That is the question: I am not asking for explanations or demonstrations of other code that might or might not have the issue of unexpected cleanup because of the way that State.Terminated is designed or called.
          Last edited by koganam; 03-08-2017, 07:43 PM.

          Comment


            #6
            Hi koganam,

            I want to clarify to make sure I'm understanding your inquiry correctly.

            You are asking why the example in the UserControlCollection does not use a flag to know when to remove objects from the chart?

            The UserControlCollection automatically removes items from the chart, where as the other examples are modifying the WPF elements of the chart directly without the UserControlCollection.

            Because this is automatically removing items, all that is needed to be removed is the event handlers which should be removed if the object they are added to is not null.

            No flag is necessary because there is no need to know whether the added items should be removed or displayed as this is all automatic.

            However, when modifying the chart directly, the script would need to remove any added objects itself, and would need to know whether or not the items it adds should be visible.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by knowmad, Today, 03:52 AM
            0 responses
            17 views
            0 likes
            Last Post knowmad
            by knowmad
             
            Started by tradingnasdaqprueba, Today, 03:42 AM
            0 responses
            12 views
            0 likes
            Last Post tradingnasdaqprueba  
            Started by Haiasi, 04-25-2024, 06:53 PM
            4 responses
            63 views
            0 likes
            Last Post effraenk  
            Started by ccbiasi, 11-23-2017, 06:06 AM
            5 responses
            2,213 views
            0 likes
            Last Post leodavis  
            Started by kujista, Yesterday, 12:39 AM
            2 responses
            14 views
            0 likes
            Last Post kujista
            by kujista
             
            Working...
            X