• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ChartControl and State.Terminated

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

    ChartControl and State.Terminated

    With the simple sample code attached...

    How can I be assured that the ChartControl.Dispatcher is Always run in State.Terminated?

    I'm finding ChartControl is "Always" Null, and the Dispatcher is Never Run, when closing the chart window or workspace...

    Please Advise?


    Attached Files
    -=Edge=-
    NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

    #2
    Hello Edge,

    Thank you for your response.

    ChartControl becomes null on the window being destroyed before the indicator reaches the Terminated State.

    You can use OnWindowDestroyed to run tasks when the window is closed. You can find more details at the following link: https://ninjatrader.com/support/help...wdestroyed.htm

    Please let me know if you have any questions.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PatrickH View Post
      You can use OnWindowDestroyed
      Is OnWindowDestroyed available in an Indicator Script?

      How would you suggest always making sure anything created using chartcontrol's dispatcher, from an indicator or strategy, always get's properly disposed of before the window or workspace is closed?


      Last edited by -=Edge=-; 03-15-2018, 03:16 PM.
      -=Edge=-
      NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

      Comment


        #4
        Hello,

        Thank you for the question.

        OnWindowDestroyed is not available in an indicator but would be in an addon which can observe the chart window. I believe this comment likely needs more context, OnWindowDestroyed could be used if you needed to do logic after a window closes specifically. This is not helpful in the context of removing controls, that is not necessary for a closed window at all. The items that would matter for a closed window would be to stop any timer/looping logic that may hold up a thread, or remove event subscriptions (if any). The remainder of the controls once dissociated should be disposed of with the window.

        In this case, State.Terminated should be run when closing the window but if you are trying to remove controls, in that case, it really wouldn't matter as the window and controls are being disposed of. The dispatcher would likely see a null reference as that is async.

        If you absolutely need to know when the window is closed outside of State.Terminated, you could likely instead subscribe to the parent windows Closed event in that indicator and then use that. The ChartControl and other items should still be null so I don't really see any solution as a closed window doesn't really need controls removed from it. State.Terminated would more or less be to handle cases when you remove the indicator but the chart is still open.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          The items that would matter for a closed window would be to stop any timer/looping logic that may hold up a thread, or remove event subscriptions (if any).
          Exactly.. Most anyone adding a button, or context menu, are going to have some type of event(s) assosiated to it..

          One button, one event.. probably not such a big deal.. Two Buttons, two event's?? Three Events.. where's the line?

          My personal experience, in some cases thru the use various windows, buttons, checkbox's, context menus, or whatever...

          It is not difficult (nor uncommon) to have couple dozen events and/or whatever.. that really should be disposed of on exit..

          If these are created within a ChartControl.Dispatcher, you need ChartControl available to Dispose...

          I've tried your suggestion, using the closing events for Windows, NTWindows, Charts, and the unload for ChartTab even...

          But so far have been unsuccessful finding anything that will work in a multi window multi tab enviroment...


          Could you please provide an example of NinjaTraders recomended way of disposing these events on a tab, window, and workspace close?


          Let's try to keep things simple... Please use NinjaTraders Help Guide Example for a UserControlCollection located here...

          https://ninjatrader.com/support/help...collection.htm

          Those events never get disposed of on a tab, window, or workspace close..


          Again.. Maybe two is no big deal.. but when you start talking dozen, and you start multiplying that by dozens of tabs.. I think we've reached that line..



          -=Edge=-
          NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

          Comment


            #6
            Hello,

            Thank you for the reply.

            In contrast to the UserControlCollection, I do see that the events are removed in the situations that matter such as removing the indicator from the chart while the chart remains open. When the window or tab is closed, there is no way to use the button again as the chart and controls were disposed of so there would be no possibility of the event being used going forward from that point. An event subscription can prevent GC, However, as soon as the object publishing the event (ChartControl) is eligible for collection, this ceases to be a problem.

            In terms of the sample, I don't see anything wrong there because the window was closed those are no longer relevant or associated with any new instance.

            In this case, it would matter if there was logic still happening in that event and it could not be stopped. For example, a background thread would need to be stopped before closing the window. In that case, the window closed events could potentially be used or State.Terminated. It wouldn't be a subscription to the ChartControl so it wouldn't matter that the ChartControl is null specifically.

            In a separate case that you are using events that stem from the ChartControl specifically and cannot unsubscribe and they are long running, you could likely use a bool from Terminated to signal them to exit or return while true.

            In your tests are you seeing a specific problem with something you are trying or is this just an observation and a hypothetical question? If you have seen a problem, it would be more beneficial to provide a specific sample of that problematic situation for further review. I would expect the outcome of your first sample based on what actions are being completed.

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              I do see that the events are removed in the situations that matter
              heh.. interesting defense.... (he never breaks the law when it matters.. )

              Originally posted by NinjaTrader_Jesse View Post
              In your tests are you seeing a specific problem
              No.. no problems, traced some serialization issues the other day, back to the code not being run in State.Terminated due to null ChartControl..

              I could swear all this was working way back.. I'm pretty a.d.d about disposing, cleaning things up, and trying to release that memory back!

              At first I thought this might be why when I closed a workspace, that 90% of the memory being used by that workspace was not being released..

              But with a little more testing... I can pretty much duplicate that issue with couple dozen charts and default indicators, so guess I'll let it be..



              -=Edge=-
              NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RandanAL, Today, 05:13 PM
              0 responses
              1 view
              0 likes
              Last Post RandanAL  
              Started by timko, Today, 05:06 PM
              0 responses
              3 views
              0 likes
              Last Post timko
              by timko
               
              Started by ballboy11, Today, 01:04 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by Linda1507, Today, 01:22 PM
              2 responses
              15 views
              0 likes
              Last Post Linda1507  
              Started by ntbone, Today, 12:15 PM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X