No announcement yet.

Partner 728x90


Cleaning up resources in an addon

  • Filter
  • Time
  • Show
Clear All
new posts

    Cleaning up resources in an addon

    The problem I am having is occurring because the addon is being terminated and restarted.

    I believe this is occurring because the NinjaScript editor is opening and this forces a recompile when it detects changes to the cs files in the custom folder.

    please correct me if I'm wrong and the ninjascript editor does not have to be open for this, and it is just based on detecting a file system change even if the editor is closed.

    so my questions are:

    1) how can I call some cleanup code when the addon is about to terminated? would onstatechange terminated work?

    2) are there any other cirumstances where an addon might be restarted?

    Hello NinjaCustomer,

    Just to clarify, the Addon class itself will have a single instance and will be terminated and recreated when compiling.
    The the OnWindowCreated() / OnWindowDestroyed() also will trigger for all windows when a compile occurs.

    However, any window that is created from the Addon (such as when clicking the Addon name in the Control Center > New menu) is a separate class from the addon will not be terminated.

    NinjaTrader does automatically trigger a compile anytime the NinjaScript Editor is open and a change is detected in a .cs file.
    There will not be an automatic compile if the NinjaScript Editor is closed.

    If you have a window that is opening, I recommend cleaning up Window class resources when the Closing event for the window is triggered.

    If you do not have a window opening, cleaning up Addon class resources in OnStateChange when State is State.Terminated is fine to do.

    Starting NinjaTrader and compiling are the two triggers that will cause an addon instance to be created.

    NinjaTrader will not automatically create an
    Last edited by NinjaTrader_ChelseaB; 05-21-2019, 01:08 PM.
    Chelsea B.NinjaTrader Customer Service


      what triggers the automatic compile then? definitely ninjatrader will compile - I can see it in the ninjascript editor animation. What I do is I edit a copy of my .cs files outside of the documents folder then i copy them into custom when I want to "deploy" them to ninjatrader. But doing so will trigger the automatic compile, the animation starts and i hear a compile finished sound.

      I do have a print message that is displayed when Terminated is called in onstatechange, however this mesage only gets printed upon initial loading of ninjatrader , not when an automatic recompile is triggered (however I can see from other print messages that the addon did get reloaded).. so it doesn't seem I can rely on terminated. Do you think onwindowdestroyed will probably get called for sure before the addon is reloaded?


        Hello NinjaCustomer,

        The NinjaScript Editor window itself has special code to monitor for file changes on a timer and trigger a compile when it is open if changes are detected.

        If all NinjaScript Editors are closed, the compile will not be triggered.

        The states of all addon classes will be terminated and OnWindowDestroyed() / OnWindowCreated() will be triggered for all open windows. (This is so you can add items to the Control Center New menu if you want after a compile)

        Let me know if you want a video demonstrating this.
        Chelsea B.NinjaTrader Customer Service


        Latest Posts


        Topics Statistics Last Post
        Started by John Bogui, Today, 03:57 AM
        2 responses
        Last Post John Bogui  
        Started by Dolfan, 12-30-2020, 12:04 PM
        9 responses
        Last Post timko
        by timko
        Started by condorhero, Yesterday, 09:28 PM
        0 responses
        Last Post condorhero  
        Started by MaraAnkh, Yesterday, 08:44 PM
        0 responses
        Last Post MaraAnkh  
        Started by wooofad, Yesterday, 05:38 PM
        0 responses
        Last Post wooofad
        by wooofad