Performance drop while creating over 3000 object!

Posted on 2004-03-25
Medium Priority
Last Modified: 2010-04-15
have made a custom user control that works like a grid, each cell on the grid is represented by a another custome control, which is just a box with a text drawing in it!(using Graphics) now, the grid reads the info from a dataset and created instances of those cell objects, the problem is it takes up to 20 seconds to load. would there be anyway to increase the performance of creating alot of object?

Question by:saturn_one
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Expert Comment

ID: 10681950

I would suggest creating arrays of objects

myObject[] myObj = new myObject[3000];

then multi-thread your application and only create the objects that you are viewing to start with.  Render your form.  and while the user is contemplating what to do, create other objects in bulk in several threads.

for(int i=0;i<1000;i++)
    myObj[i] = new myObject();

for(int i=1000;i<2000; i++)
  myObj[i] = new myObject();

for(int i=2000;i<3000; i++)
  myObj[i] = new myObject();

Author Comment

ID: 10682997
i'm already using an array, but when i try to multithread the application it gives me the, controls created on a thread can't be parented on another thread error!

I don't know if there is a way around that, but right now i'm just trying to increase the actual performance!

thanks for the reply.
LVL 12

Expert Comment

ID: 10684642
There is an old VB cheat I remember back from the VB4/5 days.  Instead of having controls for each cell of the grid, you only use 1 control, size it and place it over the currently selected cell so it accepts the user's inputs.  When the user moves off, put the results of the editing into the overlayed cell.

Now, this takes a bit of bookkeeping, but since the only you usually need a special control is on entry, this lets you shrink the memory usage (and load times) of the form by at least 3 orders of magnitude.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 10685630
first step is to locate where the bottleneck occurs, i would suggest the free Profiler from


it takes a bit of learning but is well worth it.

I suspect the bottleneck is the generation of the 3000+ controls, the trick here would be to make a control that looks and behaves  like 3000+ controls, well not even 3000 because only a couple of hundred could be visible at once.....separate the data from the presentation.....

Accepted Solution

eric_duncan earned 1200 total points
ID: 10691280
You could also take the approach used by most Word processors and the like by implementing the Flyweight design pattern. To quote from the GoF's Design Patterns: Elements of Reusable Object-Oriented Software, the "Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost." I have used this pattern in similar situations to great effect.

Basically, what you do is separate the properties that are common to all objects of a given class, and pass in the granular information as method parameters. So, if you were building a word processing application, you might create a single instance of a character object to draw every instance of the lowercase letter 'a', but in the method that actually outputs the letter to the screen you can pass in the formatting options (ie. bold, underlined, etc) that are relevant to the specific instance of the letter on the page. That way you can store the state information in a high-performance array and use this to update the properties of your object pool as necessary. Both dfiala13 and AdrianJMartin have hinted at this, and it is quite easy to manage once you get the core pattern set up.

You can look at http://www.dofactory.com/patterns/PatternFlyweight.aspx for an overview of the pattern.

Let me know if this helps!

Author Comment

ID: 10692039
this sounds really intresting, but i couldn't find any examples on how to use the Flyweight on windows form controls, could someone confirm that its possible to have couple windows form controls with diffrent position size and back color using Flyweight which handdle events properly too!

Expert Comment

ID: 10694803
Actually, it isn't possible to have the same instance of a windows form control in two places at once, but what you could do is create only the number of form controls you need to see at one time and change their contents as the user scrolls through the entire data set by capturing the OnScoll event. Or, if you really wanted to do a "pure" implementation of the Flyweight pattern, you could dispense with the Windows Forms Controls altogether, use a Panel Control on the form to provide a graphics surface, and create classes that would paint their contents on to the panel using GDI without using any forms controls. This is a little bit harder to implement and would only make sense if you didn't need all of the additional functionality that the Forms controls would provide, but the performance gains would be significant.

At any rate, you should be able to limit the number of objects to the small set that is visible at any given time.

Author Comment

ID: 10695549
well, my controls are already being drawn using the GDI but they still inherate windows.forms.control class, how can i capture the mouse events if i don't use the windows controls class???

Expert Comment

ID: 10697069
If you need to capture mouse events, then I would probably stick with the windows controls classes. To maximize performance, I would modify the Flyweight pattern by creating enough instances of each control to fill the visible area of the screen, use a scrollbar to navigate through the data, and capture the OnScroll event to change the state of the controls.

Expert Comment

ID: 10722732
Thanks for the points! Let me know if you have any more questions.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question