Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Thread safe brushes assignments

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

    Thread safe brushes assignments

    I have two questions:

    1)
    In the OnStateChange()...SetDefaults... function
    I write the code:
    colorRSI = new SolidColorBrush(Color.FromArgb(50, 0, 255, 0)); //crBveryDarkGreen;
    colorRSI.Freeze();
    And I am thread safe even though the color is non standard.

    Then in the OnBarUpdate() function
    I write the code.
    this.ColorRSIX = ColorRSI;
    // this.ColorRSIX.Freeze(); this freezing code is commented out
    or
    base.BarBrushes[0] = ColorRSI;


    Is this code thread safe?

    2)
    I don't find the equivalent of
    SolidColorBrush(Color.FromArgb(100, colorRSI))
    I used to be able to write
    Color.FromArgb(100, colorRSI))
    but now it gives error "No overload for method 'FromArgb' takes 2 arguments.
    How can I take an existing standard color contained in a instance of a class and get an equivalent one with altered opacity ?

    Thank you
    G
    Last edited by giogio1; 05-09-2022, 12:10 AM.

    #2
    Hello giogio1,

    Thank you for your reply.

    Anytime you create a custom brush that will be used by NinjaTrader rendering it must be frozen using the .Freeze() method due to the multi-threaded nature of NinjaTrader. If you do not call .Freeze() on a custom defined brush, it will eventually result in threading errors should you try to modify or access that brush after it is defined.

    This is noted along with further information on custom brushes inside of our help guide document regarding how to work with custom brushes: https://ninjatrader.com/support/help...th_brushes.htm

    ColorRSI is a brush already, so what I would suggest is to create a private brush variable at the class level, for example: private Brush ColorRSI = new SolidColorBrush(Color.FromArgb(50, 0, 255, 0));

    To create the custom opacity brush once in State.DataLoaded:

    Brush temp = ColorRSI.Clone(); // copy current brush
    temp.Opacity = 100.0/100.0; // 100% opacity
    temp.Freeze(); // required to freeze
    ColorRSI = temp; // assign 100% opaque brush to ColorRSI brush.


    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Hello
      I applied the suggested code in this function which is called at the end of OnBarUpdate();
      I get the error in the picture "Cannot Set a property on object because it is in a read-only state. See pic.
      What do you think the problem is?
      Thank you!
      G


      Click image for larger version

Name:	nt8 - because it is in a read-only state.png
Views:	129
Size:	56.9 KB
ID:	1201005

      Comment


        #4
        Hello giogio1,

        Thank you for your reply.

        I'm not showing that you're cloning the prior brush to the temporary barOpaque brush variable, but directly assigning it instead.

        Try:

        Brush barOpaque = BarColorX.Clone();

        That way you're copying it instead of assigning it.

        Please let us know if we may be of further assistance to you.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        637 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        366 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        571 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X