Aug 11, 2007

Faster .Net applications startup - Tips and Tricks

Intro:
There is this dim end user expectation, which says: "I want to launch my application and see its first user interface as soon as I double click on its icon in my Windows system".

For Native Win32 GUI applications, this is the way it has been for many years (except for very large applications that need to load hundreds of libraries...), but for .Net forms, this might be a developer's nightmare.

You design your Form, add some controls to it. Then run the application. Well, you become disappointed when you see that even on fastest machines the whole process of controls painting themselves into view is visible to the user's eyes with flickers. This is a certain drawback for your application. It makes you and the end user to think that .Net GUI applications are just bloated, heavy and useless.

Good news is that it is not like that at all. There are certain tricks in any development environment and language that improves application performance by orders of magnitude.

For me, I have used 3 simple tricks to get that speed:

1- Use Delayed Initialization:

You want your managed application to come into view just as fast as a native application does. But it is not like that in most managed cases. Although for an empty form in most cases "it is". So, what I do is to avoid the CPU and RAM overhead caused by initialization of controls at the start-up time for my application.

By default, Visual Studio inserts the "InitializeComponent();" call in the constructor for each form. If your form is going to be the start-up form (Main Form), this method will really have an impact on its display delay.

As a matter of fact, we can remove this call and insert it somewhere so that the main form wont delay its display because of this method call. I have done this in "Application.Idle" event.

So, instead of this code:
//.................................
public Form1()
{
InitializeComponent();
}
//...............................

I use this code for faster start up:

public Form1()
{
/* Preventing a very ugly fact of Managed applications, and that is
avoiding the user from seeing the controls (TextBox, Labels,...) painting themselves and their backgrounds when we run applications:
*/

this.Text = "Initializing...";

this.Size = new Size(687, 406); /* We do this to avoid form's resizing upon its initialization. */

Application.Idle += new EventHandler(Application_Idle);

}
bool isInitilized = false;
void Application_Idle(object sender, EventArgs e)
{
if (isInitilized)
return;
/* Even avoiding this method call in the application start up will be great! */
Application.EnableVisualStyles();

// Now is the time to initialize all components.

InitializeComponent();

// Avoid re-initialization:

isInitilized = true;
}
//-----------------------------------------

This simple trick will really boost the start-up time and display of your application even in cold start up scenarios. It might not always work, but in most cases it is just a matter of cutting and pasting some start up codes.

2- Use Ngen.exe

This is going to really help. I try to use it in most of my release assemblies and combined with tip #1, it makes your application to perform almost like its native equivalents.

3- Use a splash screen:

Finally, if your application is so heavy and bloated that you cannot avoid displaying its first form without performing heavy disk, network, or databases I/O operations, then try using a very lightweight splash form generated from a separate thread.

Do never forget that the main idea of how your application performs lies in the way your main first forms show up on the screen after a user execution of your software. For many, just a fast start-up application means a fast application overall, like Paint.net .

Hope this article has helped you a little in your .Net application development.

Download sample assemblies...