Link to home
Start Free TrialLog in
Avatar of saturn_one
saturn_one

asked on

Performance drop while creating over 3000 object!

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?

Avatar of _TAD_
_TAD_



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.


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


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



<thread3>
for(int i=2000;i<3000; i++)
  myObj[i] = new myObject();
Avatar of saturn_one

ASKER

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.
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.
first step is to locate where the bottleneck occurs, i would suggest the free Profiler from

http://www.compuware.com/products/devpartner/profiler/default.asp


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.....
ASKER CERTIFIED SOLUTION
Avatar of eric_duncan
eric_duncan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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.
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???
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.
Thanks for the points! Let me know if you have any more questions.

Eric