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

Accessing Array from OnRender causes "thread cannot access this object" error

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

    Accessing Array from OnRender causes "thread cannot access this object" error

    Hi guys,

    can you tell me why it throws the error "Error on calling 'OnRender' method on bar 2999: The calling thread cannot access this object because a different thread owns it."?

    I have an Array of Brushes which gets accessed in a method that is called by OnRender. And whenever it does it, it throws the above error. When I access the array directly in OnRender, it still throws the error. When I access the array in OnBarUpdate, however, it doesn't throw the error and accesses the array's brush accordingly. My guess is that OnRender uses a different thread, but why it only happens with brush arrays, and how to solve it, I have little clue. Please advise me.

    Greetings

    #2
    Hello seykool,

    Thanks for your post.

    OnBarUpdate is generally on an instrument thread. OnRender will reside on a UI thread.

    Are these brushes in your array custom brushes? If so, are you freezing them?

    Are you properly disposing SharpDX brushes as well?

    Please see the sections "Understanding Custom Brushes" and "Using advanced brush types" for more details:



    If the matter is still unresolved, could you implement just enough code in a separate script and attach it here so I can better see what you are doing?
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,
      normally I do this. Nevertheless, I oversaw to freeze the Brushes of the array, this time. That has indeed been it. Thank you!

      Comment


        #4
        If I instantiate and initialize a SharpDX.Direct2D1.SolidColorBrush in a method and only use it there, do I have to dispose it thereafter?

        Comment


          #5
          Hello seykool,

          If you are calling your own drawing methods in OnRender, and you have brushes created within the scope of those custom methods, those brushes should be disposed at the end of that method. (Similarly, if you have DX brushes created in OnRender's scope, they should be disposed at the end of OnRender.)

          Alternatively, you can dispose and recreate resources in OnRenderTargetChanged, and keep those brushes as private properties. This is better on performance and you would not have to dispose those resources elsewhere, unless you needed to update them.

          Code:
          public override void OnRenderTargetChanged()
          {
            // if dxBrush exists on first render target change, dispose of it
            if (dxBrush != null)
            {
              dxBrush.Dispose();
            }
          
            // recalculate dxBrush from value calculated in OnBarUpdated when RenderTarget is recreated
            if (RenderTarget != null)
              dxBrush = brushColor.ToDxBrush(RenderTarget);
          }
          OnRenderTargetChanged() - https://ninjatrader.com/support/help...getchanged.htm
          JimNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by andrewtrades, Today, 04:57 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          6 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          7 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X