• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1656
  • Last Modified:

Slow loading of a ListView on first Display - C# VS2008

Using VS2008. C#

I have a form with a ListView on it.

I create the form, and then load around 30,000 items into the listview from the database.  

The loading takes less than a second, but the first time I call Show(),  on the form, it takes around 5 seconds for the form to come up.  The next time do a Show(), it comes up immediately.  

What can I do, if anything, during the initialization phase to make it ALWAYS come up in less than a second?  Is there some way to pre-load the graphics or run the show() invisibly?  
1 Solution
Are you loading the items on form load and syncronously?

Why not try loading the items aysncronously, and simply informing the user with a loading message or something.  This way the Form opens and the thread it uses has no job but to open the window.  The separate thread it will use to load the list box will not affect the form showing.

Check http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23139019.html

I agree with andrew. If you are populating 30000 records, it means a delay will occur in any case if you do this within a single thread.
Fernando SotoRetiredCommented:
Hi fbk2000;

If I understand you correctly the first time you create and use the Show() method on the form takes about 5 seconds and every other time after that it comes up immediately. This is the way .Net works. When you first start your application the .Net JIT compiler compiles just enough of the application to display the first form. As you create and open other forms and classes the JIT compiler needs to compile them first and then display it. Once that new form or class has been compiled into native code it is keep in memory for later use. Therefore the next time you use the form the .Net Framework needs to display that form it knows it is already compiled and gets it from memory and this is the reason why it takes longer on the first Show().

If you need to improve performance on initial startup of forms look at using ngen utility. The ngen utility will compile the .Net exe's (Microsoft Intermediate Language code) to native code which means no need for JIT.

Ngen utility:
The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead using the just-in-time (JIT) compiler to compile the original assembly.


fbk2000Author Commented:
Please read the question thoroughly before answering.

"The loading takes less than a second...."   Loading is not the issue, nor is the database query time, etc.  All the data gets into the control immediately. So loading Asynch is not really relevant; moreover, I would need this control ready to run as soon as the user  sees the form.

Fernando is correct.  I was able to fake out the JIT, by showing the form and then immediately hiding it during load.  The reason the native code was not already compiled during the showing of the main form (it would normally be the same code for the show() method)  is because the main application form is a WPF Window, which of course is a completely different object than the WinForm.


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now