473,408 Members | 2,477 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,408 software developers and data experts.

simple app needs optimization?

Hi ,

i made this simple little app that displays all .NET colours and
handles resize and mouse events.

But if one tries to resize the app , it mucks up and performs
disgustingly slow.

Heres the source:
http://gidsfiles.googlepages.com/ColorBox.cs
http://gidsfiles.googlepages.com/FrmColours.cs
Thanks so much

Gideon

May 20 '07 #1
8 1499
Gideon,

Well, a few things.

One, I would definitely set the style to have all painting done in
WmPaint and for the optimized double buffer.

Second, you are creating a lot of resources which you are not disposing
of correctly. Fonts, Brushes, Pens, Graphics all implement IDisposable and
you should be disposing of them properly when you are done with them.

Third, the event handlers for the mouse down and whatnot should not be
creating graphics instances and then drawing on them. Rather, you should
set some state that your paint event handler can use to determine what to
paint, and then call Invalidate.

Lastly, your paint event handler should draw on the Graphics image that
is being passed to OnPaint (through the PaintEventArgs instance) and not
call CreateGraphics. A device context is already created for you, there is
no need to create another one.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"giddy" <gi*******@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
Hi ,

i made this simple little app that displays all .NET colours and
handles resize and mouse events.

But if one tries to resize the app , it mucks up and performs
disgustingly slow.

Heres the source:
http://gidsfiles.googlepages.com/ColorBox.cs
http://gidsfiles.googlepages.com/FrmColours.cs
Thanks so much

Gideon
May 20 '07 #2
On Sun, 20 May 2007 00:55:06 -0700, giddy <gi*******@gmail.comwrote:
i made this simple little app that displays all .NET colours and
handles resize and mouse events.

But if one tries to resize the app , it mucks up and performs
disgustingly slow.
On what kind of hardware?

For what it's worth, I tried compiling and running your program just for
grins. While I agree wholeheartedly with everything Nicholas wrote, I
didn't see anything that I'd consider to be glaring performance problems
and the program runs fine speed-wise on my computer. Some of the things
he mentions may improve efficiency slightly, but there don't seem to be
any fundamental issues dealing directly with execution speed.

Maybe you can use more specific descriptions to explain the problem you're
having, rather than the rather vague "it mucks up and performs
disgustingly slow". Those aren't what I'd consider technically precise
terminology.

Pete
May 20 '07 #3
As a side note,
you don't need to Dispose the e.Graphics object (and this probably
raises an Exception too, I think).

I used to have similar problems with Graphics-intensive applications,
but now I've learned that the most important of all things you wrote
was to use e.Graphics and always Dispose things readily - I've noticed
before (my mistakes) the memory of my faulty applications can go up
really fast, but Dispose can resolve that, while e.Graphics is
absolutely necessary to ensure that there are no flashing forms, which
was due to the slowness of using CreateGraphics().

Freiddie
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/
http://scilearn.blogspot.com/

May 20 '07 #4
Bravo, Peter!
:-)
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Peter Duniho" wrote:
On Sun, 20 May 2007 00:55:06 -0700, giddy <gi*******@gmail.comwrote:
i made this simple little app that displays all .NET colours and
handles resize and mouse events.

But if one tries to resize the app , it mucks up and performs
disgustingly slow.

On what kind of hardware?

For what it's worth, I tried compiling and running your program just for
grins. While I agree wholeheartedly with everything Nicholas wrote, I
didn't see anything that I'd consider to be glaring performance problems
and the program runs fine speed-wise on my computer. Some of the things
he mentions may improve efficiency slightly, but there don't seem to be
any fundamental issues dealing directly with execution speed.

Maybe you can use more specific descriptions to explain the problem you're
having, rather than the rather vague "it mucks up and performs
disgustingly slow". Those aren't what I'd consider technically precise
terminology.

Pete
May 20 '07 #5
hi,

Thanks Nicholas. That definately improved a lot of things.

To Peter Duniho ,
umm My Pc : 1.8 GHz , 512 RAM. I'm sorry but i really thought you'd
notice. When i resized , the app would freeze and i could see the
Location of the colour boxes changing one by one. Oddly , i got the
program on my laptop [1.6Ghz 256RAM] and it performed ok , much like
what you *probably experienced. So i'm guessing it was either my pc
then or some code that i changed before posting here! lol

It performs *almost performs perfect now :

Somehow when i _resize_ the scrollable space doesnt adjust correctly.
My calculations are perfect. I Set AutoScrollMinSize.Width to the
current cient width , and the height to the number of rows X (space +
boxHeight).Once you resize , the scrollbar adjusts with this odd
additional space , if you scroll on the bottom you'll see a that
space.

Also , when i click a box , its selected property turns true and it
highlights.But when one selects a box i want the last one to
deselect , so what would my best option be? Setting up an event so
everytime a box is clicked i can store it and then set Selected=false
when another is selected??

Heres the source(updated):
http://gidsfiles.googlepages.com/ColorBox.cs
http://gidsfiles.googlepages.com/FrmColours.cs

Thanks so Much

Gideon

May 27 '07 #6
On Sun, 27 May 2007 12:48:56 -0700, giddy <gi*******@gmail.comwrote:
[...]
Somehow when i _resize_ the scrollable space doesnt adjust correctly.
My calculations are perfect.
I beg to differ. :)
I Set AutoScrollMinSize.Width to the
current cient width , and the height to the number of rows X (space +
boxHeight).Once you resize , the scrollbar adjusts with this odd
additional space , if you scroll on the bottom you'll see a that
space.
I saw a variety of problems with the code you posted. Key problems though
included *two* different ways for you to leave the OnResize() method
having called SuspendLayout() but without calling ResumeLayout(), as well
as inefficient and buggy calculations for the rows and columns. I didn't
rewrite it completely, but here's a version that at least corrects what I
felt were the most problematic issues:

protected override void OnResize(EventArgs e)
{
if (ClientSize.Width == 0)
{
return; //incase the form is minimised.
}

cols = (int)Math.Ceiling(((double)ClientSize.Width / (space
+ szClr.Width)));//the cols that can fit
rows = (int)Math.Ceiling((double)_clrBoxes.Count / cols);//the
number of rows NEEDED
//reposition all boxes
this.SuspendLayout();
Point pt = new Point(space, space);
int b = 0;//box count
int xMax = ClientSize.Width - szClr.Width;

while (b < _clrBoxes.Count)
{
if (pt.X >= xMax)
{
pt.X = space;
pt.Y += (space + szClr.Height);
}

_clrBoxes[b].Location = pt;
pt.X += space + szClr.Width;
b++;
}

this.ResumeLayout(true);
base.OnResize(e);
}

Note that, among other things, when you handle everything else correctly
the issue of recursive calls does not exist, and you do NOT need to set
the AutoScrollSizeMin explicitly. Because the things affecting the
minimum size are controls, the Form automatically updates the scroll bars
to account for changes in the location of the child controls.

Also, note that in the new code, layout of the child controls always
begins at (0,0) rather than at the scroll position. The Form's own layout
code corrected for the erroneous use of the scroll position while laying
out the child controls, but I believe that in the long run no good could
have come from doing it that way.

There are other subtle differences between the code above and the code you
wrote, but I don't feel a need to enumerate each and every difference.
Suffice to say, where a difference exist, it's my opinion that my code is
better. :)
Also , when i click a box , its selected property turns true and it
highlights.But when one selects a box i want the last one to
deselect , so what would my best option be? Setting up an event so
everytime a box is clicked i can store it and then set Selected=false
when another is selected??
I see at least two options. One is to maintain a form-level variable that
keeps track of the current selection, so that when you select a new child
control, you can deselect the old selection. The other, and IMHO the most
appropriate method given that you are using Control-derived contents for
the form in the first place, is to take advantage of the fact that
controls already have a concept of having "focus", and automatically deal
with acquiring and losing focus.

Using the second method, you would simply allow the form itself to deal
with actually maintaining the focus state and then check the Focused
property to determine which font to use to draw the control. You'll need
to add handlers for the GotFocus and LostFocus events to invalidate the
control so that it will be updated, but otherwise everything else is
handled for you. You don't have to handle mouse-tracking, using the tab
key to change focus from one control to the next works automatically, and
of course you don't need the _selected field or Selected property doing it
that way.

Pete
May 27 '07 #7
hi,

Sorry for the late reply.

your code works like a charm! And i love this line:
int xMax = ClientSize.Width - szClr.Width;

I dont need the cols and rows then. It took me a good lot of hours to
come up with my original calculation , dont know how you figrued it
out so quick! =O

I still have'nt completed the focus thing , this is just one of my pet
projects , but i'll update the link to the new code when i do. =P

Thanks so much

Gideon

Jun 1 '07 #8
On Fri, 01 Jun 2007 03:00:53 -0700, giddy <gi*******@gmail.comwrote:
your code works like a charm! And i love this line:
int xMax = ClientSize.Width - szClr.Width;
Me too. I'm happy you enjoyed it. :)
I dont need the cols and rows then. It took me a good lot of hours to
come up with my original calculation , dont know how you figrued it
out so quick! =O
The same way you get to Carnegie Hall.

("Practice, years of practice"...) :)
I still have'nt completed the focus thing , this is just one of my pet
projects , but i'll update the link to the new code when i do. =P
Thanks for letting me know it was helpful. Glad I could assist.

Pete
Jun 1 '07 #9

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

Similar topics

9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
4
by: Vicky | last post by:
Hi, my friends and i have been confused by this problem. On a veriety of systems we have tested compiling a simple hello word applicaation to check optimisation of C++ in 2003 VS.NET. However...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
1
by: Tim | last post by:
Here is a list of a few very simple tips for optimizing your php/mysql applications. Keep these in mind while developing to survive the digg-effect. ...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
0
by: giddy | last post by:
Hi , i made this simple little app that displays all .NET colours and handles resize and mouse events. But if one tries to resize the app , it mucks up and performs disgustingly slow. ...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.