Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best way to dispose of a SharpDX brush used in a list

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

    Best way to dispose of a SharpDX brush used in a list

    I have a list that contains objects and I wanted to store the SharpDX brush color in the list so that instead of doing a ternary in OnRender to figure out the color, I can just call the correct color from the list



    HTML Code:
            public class FVG
            {
                public SharpDX.Direct2D1.Brush         DxBrushGap;    
                public SharpDX.Direct2D1.Brush         DxBrushGapBorder;    
                public SharpDX.Direct2D1.Brush         DxBrushIconBorder;    
    
                public int                    period;                        //        period for multi FVGs
                public int                    type;                        //        BULL        BEAR
                public bool                    showGap;                    //        YES            NO
                public int                    status;                        //        OPEN        InTRADE        EXPIRED        CLOSED
    ​

    it works for coloring my objects, but what is the best way to dispose of the brush.

    This is what I use for other brushes, but wasn't sure how to dispose the one that is in the list

    HTML Code:
            if (DxBrushWhite != null)            DxBrushWhite.Dispose();            
            DxBrushWhite            = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.White);
    ​


    here is the ternary code i was using, but i know I'm not supposed to do too much math inside OnRender.

    HTML Code:
          RenderTarget.FillRectangle(DxRectangle, bull ?     setupQuality == GOOD ? DxBrushCyan      : DxBrushTeal25  :
                                                             setupQuality == GOOD ? DxBrushMagenta     : DxBrushCrimson25);    
    ​


    Help please....
    Last edited by cre8able; 05-30-2024, 03:43 PM.

    #2
    Hello cre8able,

    The Brush should be disposed of in OnRenderTargetChanged.

    Below is a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      hey Chelsea

      I tried to follow the example, but am still confused on how to dispose of the dxBrush that is a part of my list. Here is a simplified version of what I am trying to do.

      I create a list of rectangles with 3 properties. 1 property is the fill color which is a dxBrush


      First I instantiate the brushes... by the way, I'm not a C# programmer so if I get syntax wrong, I appologize

      HTML Code:
              SharpDX.Direct2D1.Brush DxBrushWhite;    
              SharpDX.Direct2D1.Brush DxBrushGreen;    
      ​
      then I create my class list where I create rectangleFillBrush but it's a part of the list, not a global brush.

      HTML Code:
              public class rect
              {
                  public SharpDX.Direct2D1.Brush        rectangleFillBrush;
                  public int                            rectangleWidth;
                  public int                            rectangleHeight;
              }            
              private List<rect> rectOpen                     = new List<rect>();
      ​
      In OnBarUpdate, I "decide" what color the fill is for each rectangle. Lets pretend I have other code that has created 20 rectangles in my list.

      I cycle through each rectangle and assign is fill color

      HTML Code:
              protected override void OnBarUpdate()
              {            
                  for (index = rectOpen.Count - 1; index >= 0 ; index--)
                          rectOpen[index].rectangleFillBrush = rectOpen[index].rectangleHeight > 10 ? DxBrushWhite : DxBrushGreen;
      ​
      Then I would use OnRender to draw and fill in my rectangles.

      I know !'m supposed to create and dispose of the brushes in OnRenderTargetChanged. I know how to do that with a "stand alone" brush like DxBrushWhite.


      HTML Code:
              public override void OnRenderTargetChanged()
              {
                  
                  if (DxBrushWhite != null)            
                      DxBrushWhite.Dispose();            
                  DxBrushWhite = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.White);
                  
                  if (DxBrushGreen != null)            
                      DxBrushGreen.Dispose();            
                  DxBrushGreen = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Green);
                  
                  if (rectangleFillBrush != null)
                      rectangleFillBrush.Dispose();        
      ​
      But I can't dispose of "rectangleFillBrush" because it's a list field, not a general variable like DxBrushWhite.

      I guess the first question is do I have to dispose of and recreate the rectangleFillBrush in the list? If not, then I'm fine


      If I do have to dispose and re-create in OnRenderTargetChanged, how can I do that if its a part of a list... and how can I do it without losing the current color of the rectangleFillBrush for each of my different rectangles?

      In my actual application, I have 100's of rectangles and some are "closed" so I can't re-run the OnBarUpdate code to find the color again.


      I have been using a variable in the list that I then use to "pick" the color during OnRender, like this:

      HTML Code:
                          RenderTarget.DrawRectangle(DxRectangle, gapBorder == RED ? DxBrushRed : gapBorder == GREEN ? DxBrushGreen : DxBrushTransparent, 1);                    
      ​

      But I since I'm supposed to minimize OnRender calcs, and the color doesn't change very often, I wanted to make the color a property in the list.

      what's the best way to solve my problem?

      thanks

      Comment


        #4
        Hello cre8able,

        foreach (rect thisRect in rectOpen)
        {
        if (rect.rectangleFillBrush != null)
        {
        rect.rectangleFillBrush.Dispose();
        }
        }

        Note, the ReuseDxBrushesLoopDictionaryExample example I provided you uses a dictionary and loops through the dictionary in OnRenderTargetChanged().
        You might decide to use this same structure and logic.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          thanks Chelsea. I will try that

          Comment


            #6
            Hey Chelsea

            I tried what you said. I put the foreach loop into OnRenderTargetChanged(). When the chart first renders, it works great. But as soon as I scroll, the brushes get disposed and the rectangles no longer render.

            I don't know ho to re-render each "rect.rectangleFillBrush" with the value it originally contained after I dispose of it.




            help again... please

            Comment


              #7
              Hello cre8able,

              Are you creating new brushes for the new RenderTarget.

              Have a look at the example I have provided you on lines 162 to 158.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                hey Chelsea

                I wasn't recreating the brushes so it makes sense that they disappeared.

                But I'm stuck on how to recreate the brush for my list instead of a dictionary

                the dictionary version had ".Value" that i can't figure out how to do in the list

                Dictionary version:
                HTML Code:
                                foreach (KeyValuePair<string, DXMediaMap> item in dxmBrushes)
                                {
                                    if (item.Value.DxBrush != null)
                                        item.Value.DxBrush.Dispose();
                
                                    if (RenderTarget != null)
                                        item.Value.DxBrush = item.Value.MediaBrush.ToDxBrush(RenderTarget);                    
                                }
                ​

                List version:
                HTML Code:
                     foreach (rect thisRect in rectOpen)
                     {
                          if (rect.rectangleFillBrush != null)
                          {
                               rect.rectangleFillBrush.Dispose();
                          }
                     }​
                how do I recreate this part for a list?
                HTML Code:
                               if (RenderTarget != null)
                                        item.Value.DxBrush = item.Value.MediaBrush.ToDxBrush(RenderTarget);      ​

                Comment


                  #9
                  Hello cre8able,

                  A dictionary was used in the example to group one System.Windows.Media.Brush with it's SharpDX.Direct2D1.Brush equivalent.

                  foreach (rect thisRect in rectOpen)
                  {
                  if (thisRect.rectangleFillBrush != null)
                  {
                  thisRect.rectangleFillBrush.Dispose();
                  thisRect.rectangleFillBrush = thisRect.rectangleHeight > 10 ? DxBrushWhite : DxBrushGreen;
                  }
                  }

                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    thanks Chelsea

                    In my actual indicator the logic for the color is more complex than in my example of:

                    thisRect.rectangleFillBrush = thisRect.rectangleHeight > 10 ? DxBrushWhite : DxBrushGreen;

                    It doesn't change very often so I think I'm better off calculating the color in OnBarUpdate() and storing it in the list as non dxBrush variable. Then I can do a simple calculation to convert it to the dxBrush when I render.

                    That way all my dxBrushes are created and disposed of in OnRenderTargetChanged(), but all the calcs are in OnBarUpdate()

                    Does that sound like a better idea than having more complex calcs in OnRenderTargetChanged(),?

                    Comment


                      #11
                      Hello cre8able,

                      I think if the brushes are already being created and stored and recreated in OnRenderTargetChanged(), you should just point to the existing brush instead of duplicating them (which is redundant).

                      An easy way to do that would be to keep the brushes in a dictionary, and then select them by the key.

                      The rect object could just save the key string for the color it should use.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        got it

                        thanks ChelseaB

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        581 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        338 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        103 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        554 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        552 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X