Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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.
Independent Software Vendors: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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