Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Freeze() needed for Stroke.Opacity or Plot.Opacity?

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

    Freeze() needed for Stroke.Opacity or Plot.Opacity?

    Hi,

    The official NinjaTrader help guide warns in multiple places that a custom System.Windows.Media.Brush should be frozen using Brush.Freeze(). Here is the warning on the "Working with Brushes" help page at https://ninjatrader.com/support/help...th_brushes.htm :

    Warning: If you do not call .Freeze() on a custom defined brush WILL eventually result in threading errors should you try to modify or access that brush after it is defined.
    And on the NinjaScript Best Practices page at https://ninjatrader.com/support/help..._practices.htm there is a section on "Using WPF brushes" that has an example showing how changing the Opacity of a Brush via Brush.Opacity makes it "Customized" and would require the brush to be considered a unique, custom brush that should be frozen separately from a Brush with the same Color (with the same A, R, G, and B components) but with different Brush.Opacity values.

    Code:
    // predefined brush
    BackBrush = Brushes.Blue;
    
    [B]// if you are using a custom brush to e.g., modify the opacity
    SolidColorBrush opaqueBlue = new SolidColorBrush(Colors.Blue) {Opacity = .25f};[/B]
    
    // or just using at custom color not available in pre-defined brushes class
    SolidColorBrush coolGreen = new SolidColorBrush(Color.FromRgb(30, 255, 128));
    
    // you must freeze these brushes after they are constructed!
    opaqueBlue.Freeze();
    coolGreen.Freeze();​
    So my questions about freezing Brushes are these:

    Question #1:Is it safe to create two Strokes with the same frozen brush as shown below if the Strokes are created with different Opacity values via the Stroke constructor that has an "int opacity" input parameter like this one, where the 25 sets one Stroke's Opacity to 25% and 75 sets the other Stroke's Opacity to 75%, or does the Stroke() object internally create two separate custom Brush objects that might not both be frozen, resulting in a threading error?

    Code:
    // One properly frozen Brush, two Stroke objects created with it. Is this safe?
    SolidColorBrush mySolidColorBrush = new SolidColorBrush(Color.FromRgb(30, 255, 128));
    mySolidColorBrush.Freeze();
    NinjaTrader.Gui.Stroke myStroke = new NinjaTrader.Gui.Stroke(mySolidColorBrush, DashStyleHelper.Solid, 1, 25);
    NinjaTrader.Gui.Stroke myStroke2 = new NinjaTrader.Gui.Stroke(mySolidColorBrush, DashStyleHelper.Solid, 1, 75);
    Question #2: The "NinjaTrader.Gui.Plot" object also has an "Opacity" property just like "NinjaTrader.Gui.Stroke" object does. So long as I properly freeze a single Brush object, can I use this same Brush object to set the Plot.Brush property of two different plots, but set the Plot.Opacity property value to 25 for one Plot using "myPlot1.Opacity = 25;" but 75 for the other Plot using "myPlot2.Opacity = 75;" without getting a threading error?

    For instance, I see that NinjaTrader_ChelseaB wrote the following at https://forum.ninjatrader.com/forum/...16#post1300016 :

    Try setting the Opacity directly in State.DataLoaded.

    Plots[0].Opacity = PlotOpacity;


    Does the above line of code ever cause problems if the Brush of the Plots[0] Plot object is a custom brush with a custom color that was properly frozen?

    I ask because I have the concern that setting the Opacity public property of the Plot object might possibly create a second Brush after the fact that I would not have frozen in my code that calls it and I would have no way to freeze it because it would be internal to the Plot object.

    Thanks in advance!

    EquityTrader
    Last edited by EquityTrader; 08-03-2024, 09:01 PM.

    #2
    Hello EquityTrader,

    "Is it safe to create two Strokes with the same frozen brush as shown below if the Strokes are created with different Opacity values"

    Yes, the 'mySolidColorBrush' would not be changed when supplying this to two different stroke objects.


    "The "NinjaTrader.Gui.Plot" object also has an "Opacity" property just like "NinjaTrader.Gui.Stroke" object does. So long as I properly freeze a single Brush object, can I use this same Brush object to set the Plot.Brush property of two different plots, but set the Plot.Opacity property value to 25 for one Plot using "myPlot1.Opacity = 25;" but 75 for the other Plot using "myPlot2.Opacity = 75;"​ without getting a threading error?"

    Yes, the opacity would apply to the plot object and wouldn't changing the frozen brush.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_ChelseaB,

      Thank you very much for this important information. This is really good news. I will now stop worrying about threading errors when creating two different Plots or Strokes whose Opacity values I set to different values, but which use the same frozen custom brush.

      Thanks again,

      EquityTrader

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      20 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      119 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      63 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      45 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X