473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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
Jun 5 '06 #1
3 1715
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]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.com> wrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
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

Jun 5 '06 #2
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uT**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.com> wrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
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


Jun 5 '06 #3
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]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.com> wrote in message
news:uu**************@TK2MSFTNGP03.phx.gbl...
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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uT**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.com> wrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
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



Jun 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ross | last post by:
Building a simple PHP/MySQL database interface. All SELECT queries return: "Resource id #2" What is this Resource id? Is there a list of them described somewhere? Looked all over php.net ...
11
by: yang su | last post by:
I have a very simple program listed below, which basically tries to use resources but prevent from memory leak should exception occurs. class T is the resource and class User is to use the...
10
by: Niall | last post by:
I'm not quite sure which groups to post this to, so short of a massive crosspost, I decided on these two. I am running into troubles with resource usage of our app on Win2k and above systems. In...
2
by: Dennis Myrén | last post by:
Hi. I would need an advice here please. I have to define a set of constant key/value pairs for my C# .NET DLL component. We are talking about at least 5000 pairs (where each key is a...
1
by: Rick Gigger | last post by:
I am running a few web based applications with postgres on the backend. We have a few app servers load balanced all connecting to a dedicated postgres server. As usage on the applications...
2
by: Sugapablo | last post by:
I have a small test script connecting to a MySQL database. It seems to work, unless I try to use the resource link identifier returned by mysql_connect(); This works and returns all the rows in...
1
by: John Veldthuis | last post by:
I have a program which is using a Notify control for a tray icon. I originally had the icon for this separate as I have to change it all the time. When the icon was in the directory it worked fine...
7
by: Kurda Yon | last post by:
Hi everybody, I cannot understand the following thinks. The last line of the fillowing code produces a message about mistake (not a valid MySQL- Link resource): $link = mysql_connect(...
1
by: Michael | last post by:
Hi. Could anyone help me out with this one. I have a aspx page from where users may send invites to other users. When the user clicks the "send invite"-button an email is generated. Now...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.