No announcement yet.

Partner 728x90


unhandled exception : cannot access a disposed object. Object name:"Direct2DForm"

  • Filter
  • Time
  • Show
Clear All
new posts

    unhandled exception : cannot access a disposed object. Object name:"Direct2DForm"


    Can anyone point me in the direction of the Object name "Direct2DForm" within NT8 so that I work out why my indicator is trying to access it after it appears to have been disposed? It's not an object that I have initiated as far as I know so I'm guessing it must realte to some of the NT8 methods.


    Hello b16_aln,

    Thank you for the post.

    I am not aware what this would stem from, that could potentially be a lot of different items. Direct2d is part of the rendering framework which is used in about all NinjaScript types, based on the name potentially that is the chart. Is there something specific you are doing in code when you see this happen? If you have a set of steps that reproduces the error you could comment out code in your script to further isolate what part that's coming from.

    Based on the error of accessing a disposed object this could relate to something you are doing in State.Terminated and accessing something that is disposed like a chart object.

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


      Thanks Jesse,

      Yes, it probably is related to a chart. I'm using a dispatcher to apply the Close() method to automatically close down a chart window. I can post some of the code when I get back on my pc.

      Do I need to do anything after the Close() method to make sure that the script is stopped on the thread? From what you've posted, my understanding is this may be relating to the chart script running on, on a different thread after the dispatcher thread has closed the window?


        Hello b16_aln,

        There are not enough details here for me to make any suggestions other than to collect the specific steps you are using to generate the error and then isolate what code/state when the code is called in. That would give us more details on whats happening instead of guessing that you are accessing a disposed object. I could make a guess that when you are calling the Close method is not valid but I would need to see a specific example of where that code is being called and how it was used when it failed. It could also be that Close is not the correct way to close the window you are targeting, at this time there is not a specific documented Close for the NT window and we can only assume the WPF Close method can be used.

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



          The code i'm using is

          var window = NinjaTrader.Core.Globals.AllWindows[NinjaTrader.Core.Globals.AllWindows.IndexOf(ChartC ontrol.OwnerChart)] as Chart;
          window.Dispatcher.InvokeAsync((Action)(() =>

          The code is effective in that the window closes however it also results in the unhandled exception : cannot access a disposed object. Object name:"Direct2DForm"

          If I use Thread.Sleep() before the the Close() method it appears to not result int he error however I am aware that Thread.Sleep() is NOT a viable options it does point me in the direction that possibly the Close() is simply occuring too quickly for the rest of the code on non-dispatcher thread to catch up?


            Hello b16_aln, thanks for your reply.

            Unfortunately, there is nothing documented or supported that we can offer to help. It would take trial and error testing/debugging to get this to work properly. There is a lot of background work going on with Chart windows, indicator loading, etc that we do not have documented publicly, so it goes out of the scope of support we can provide.

            Best regards.
            Chris L.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by lokifdr, Today, 02:13 AM
            0 responses
            Last Post lokifdr
            by lokifdr
            Started by b16_aln, Today, 01:48 AM
            0 responses
            Last Post b16_aln
            by b16_aln
            Started by james272, Yesterday, 10:50 PM
            1 response
            Last Post NinjaTrader_Marco  
            Started by vaz21102, Yesterday, 09:22 PM
            2 responses
            Last Post vaz21102  
            Started by keepsimple, 08-17-2017, 09:18 AM
            6 responses
            1 like
            Last Post gubbar924