Resource usage

  • Thread starter Thread starter Steve Barnett
  • Start date Start date
S

Steve Barnett

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.FillRectangle(new SolidBrush(thisNode.EntityColor), 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
 
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(thisNode.EntityColor))
{
// Fill the rectangle.
canvas.FillRectangle(b, colourType);
}

Hope this helps.
 
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 said:
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(thisNode.EntityColor))
{
// Fill the rectangle.
canvas.FillRectangle(b, colourType);
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Barnett said:
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.FillRectangle(new SolidBrush(thisNode.EntityColor),
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
 
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]
- (e-mail address removed)



Steve Barnett said:
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 said:
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(thisNode.EntityColor))
{
// Fill the rectangle.
canvas.FillRectangle(b, colourType);
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Barnett said:
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.FillRectangle(new SolidBrush(thisNode.EntityColor),
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
 
Back
Top