Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make .NET executable to load faster in the first time

I made a simple Windows Forms executable in C# (by simple, I mean it has about 20 methods, but it's just one window), targeting .NET Framework 2.0. When the application loads, it doesn't do anything else than the default InitializeComponent(); in the constructor.

The first time I open, the application takes about 7 seconds to load in my Windows 8 here. Then it takes less than a second in the next times I open it.

In a Windows XP I tried, it takes about 30 seconds to load in the first time. A few friends of mine, when testing the application, also complain that it takes a lot of time to load in the first time (about 30 seconds too). Then it takes faster (1 or 2 seconds).

I assume this can be because .NET Framework is not loaded yet, so it takes some time to load in their machines.

Have you ever experienced the same problem with .NET applications?
Do you have any clue why this happens and how can I fix this?

EDIT - I see some people are suggesting NGEN. However, if this needs to be done in every machine that will use the application, it can't be the solution for this. I want to release my application to a large public of "common users", and that makes no sense if I require them to do some extra stuff to use my application. It's already bad enough that we are requiring the .NET Framework. My application should only be a standalone EXE without any dependencies (except for the framework).

Thank you.

like image 860
Nuno Avatar asked Mar 26 '13 23:03

Nuno


2 Answers

This is most likely caused by Just-In-Time compilation of the CIL. You can compile your code for the environment that you are running on using NGen. However, you will most likely lose the platform agnostic-ness of .Net if you go down this route.

This blog entry on MSDN explains the performance benefits of NGen in some detail, I suggest giving it a read.

Update following comments

As Lloyd points out in his answer, if you give your users an installer NGen can be run at this point for the environment that the application is being installed on.

However, if NGen isn't an option, then I'd recommend starting your application with a profiler attached. It can highlight any performance bottlenecks in your code. If you have any singletons or expensive static initializers these will be highlighted as such and will give you the opportunity to refactor them.

There are many great .Net profilers out there (see this SO question for details), personally I'd recommend having a look at dotTrace - they also offer a free trial period for a month which may be all that's required for your application.

like image 37
Rich O'Kelly Avatar answered Oct 05 '22 22:10

Rich O'Kelly


You can try pre-generating the native image using NGEN which .NET will use when your application loads.

You can find more information here - http://msdn.microsoft.com/en-GB/library/6t9t5wcf(v=vs.80).aspx

These are platform dependant and not transferable usually so you'll need to do this on each machine you deploy on.

like image 56
Lloyd Avatar answered Oct 06 '22 00:10

Lloyd