Resource usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Barnett

    Resource usage

    A question on resource usage please. I have a custom control that I paint
    myself. As part of the code, I have several places where I include code
    like:

    canvas.FillRect angle(new SolidBrush(this Node.EntityColo r), colourType);

    This fills a small rectangle using an identifying colour for the line being
    displayed. There may be any number of different identifying colours.

    Am I consuming resources that are likely to be leaked or held on to by using
    the "new SolidBrush" construct in my control? Would I be better to create a
    Brush and Dispose of it immediately after use?

    Thanks
    Steve


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Resource usage

    Steve,

    You should most definitely be disposing of the brush, just as you should
    be disposing of your canvas reference (to the Graphics object), like so:

    // Get the canvas
    using (Graphics canvas = <code to get Graphics instance here>)
    // Get the brush.
    using (SolidBrush b = new SolidBrush(this Node.EntityColo r))
    {
    // Fill the rectangle.
    canvas.FillRect angle(b, colourType);
    }

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Steve Barnett" <noname@nodomai n.com> wrote in message
    news:uVlUMtKiGH A.3956@TK2MSFTN GP02.phx.gbl...[color=blue]
    >A question on resource usage please. I have a custom control that I paint
    >myself. As part of the code, I have several places where I include code
    >like:
    >
    > canvas.FillRect angle(new SolidBrush(this Node.EntityColo r), colourType);
    >
    > This fills a small rectangle using an identifying colour for the line
    > being displayed. There may be any number of different identifying colours.
    >
    > Am I consuming resources that are likely to be leaked or held on to by
    > using the "new SolidBrush" construct in my control? Would I be better to
    > create a Brush and Dispose of it immediately after use?
    >
    > Thanks
    > Steve
    >[/color]


    Comment

    • Steve Barnett

      #3
      Re: Resource usage

      So, presumably, if my loop display 60 items on screen, I end up with 60
      brushes awaiting disposal at the end of the Paint function. I feel the need
      of a review of the code coming on... I'm a bit new to painting my own
      controls, having come from a VB6 background where my custom controls were
      usually made up of other standard controls dropped on to a form.

      Thanks
      Steve


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:uTUMU9KiGH A.4140@TK2MSFTN GP03.phx.gbl...[color=blue]
      > Steve,
      >
      > You should most definitely be disposing of the brush, just as you
      > should be disposing of your canvas reference (to the Graphics object),
      > like so:
      >
      > // Get the canvas
      > using (Graphics canvas = <code to get Graphics instance here>)
      > // Get the brush.
      > using (SolidBrush b = new SolidBrush(this Node.EntityColo r))
      > {
      > // Fill the rectangle.
      > canvas.FillRect angle(b, colourType);
      > }
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Steve Barnett" <noname@nodomai n.com> wrote in message
      > news:uVlUMtKiGH A.3956@TK2MSFTN GP02.phx.gbl...[color=green]
      >>A question on resource usage please. I have a custom control that I paint
      >>myself. As part of the code, I have several places where I include code
      >>like:
      >>
      >> canvas.FillRect angle(new SolidBrush(this Node.EntityColo r),
      >> colourType);
      >>
      >> This fills a small rectangle using an identifying colour for the line
      >> being displayed. There may be any number of different identifying
      >> colours.
      >>
      >> Am I consuming resources that are likely to be leaked or held on to by
      >> using the "new SolidBrush" construct in my control? Would I be better to
      >> create a Brush and Dispose of it immediately after use?
      >>
      >> Thanks
      >> Steve
      >>[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Resource usage

        Steve,

        Well, since the graphics instance should not have to be obtained every
        time, you would use the using statement outside of your loop.

        The brush is different. If the color is going to be the same for all of
        the items, then create the brush outside of the loop.

        Also, you could create the brush in your class (and dispose of it
        properly when your class's implementation of IDisposable is called) if you
        are going to use it often.

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m



        "Steve Barnett" <noname@nodomai n.com> wrote in message
        news:uulIGHLiGH A.4140@TK2MSFTN GP03.phx.gbl...[color=blue]
        > So, presumably, if my loop display 60 items on screen, I end up with 60
        > brushes awaiting disposal at the end of the Paint function. I feel the
        > need of a review of the code coming on... I'm a bit new to painting my own
        > controls, having come from a VB6 background where my custom controls were
        > usually made up of other standard controls dropped on to a form.
        >
        > Thanks
        > Steve
        >
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
        > in message news:uTUMU9KiGH A.4140@TK2MSFTN GP03.phx.gbl...[color=green]
        >> Steve,
        >>
        >> You should most definitely be disposing of the brush, just as you
        >> should be disposing of your canvas reference (to the Graphics object),
        >> like so:
        >>
        >> // Get the canvas
        >> using (Graphics canvas = <code to get Graphics instance here>)
        >> // Get the brush.
        >> using (SolidBrush b = new SolidBrush(this Node.EntityColo r))
        >> {
        >> // Fill the rectangle.
        >> canvas.FillRect angle(b, colourType);
        >> }
        >>
        >> Hope this helps.
        >>
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >> "Steve Barnett" <noname@nodomai n.com> wrote in message
        >> news:uVlUMtKiGH A.3956@TK2MSFTN GP02.phx.gbl...[color=darkred]
        >>>A question on resource usage please. I have a custom control that I paint
        >>>myself. As part of the code, I have several places where I include code
        >>>like:
        >>>
        >>> canvas.FillRect angle(new SolidBrush(this Node.EntityColo r),
        >>> colourType);
        >>>
        >>> This fills a small rectangle using an identifying colour for the line
        >>> being displayed. There may be any number of different identifying
        >>> colours.
        >>>
        >>> Am I consuming resources that are likely to be leaked or held on to by
        >>> using the "new SolidBrush" construct in my control? Would I be better to
        >>> create a Brush and Dispose of it immediately after use?
        >>>
        >>> Thanks
        >>> Steve
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...