NGEN` optimizes startup time. With an installer class that automates NGEN precompilation, we improve Windows Forms startup time. The code must be run automatically at install time.`Steps.` First, your installer class will need to know lots of details about the runtime and use several commands. I list some steps to prepare for automating NGEN.exe in your C# application. `Next: `This is the command line you can type into the terminal or console to run NGEN.exe at any time on your computer.`It is the essential line that our installer class must use automatically to invoke NGEN.`Module.` You need to use an installer module, add it to your assembly, and compile it. This installer module must know the full path of NGEN.exe and your assembly's name, which usually ends in .exe. These values can be found programmatically.`Go to the Project menu.` In the project menu, go to Add New Item and then select "Installer Class" from the list. Create NgenInstaller.cs. In my projects, I used this name for the installer class. `Next: `We show the method you must add to your installer class to make an installer module that runs NGEN.exe at install time.`Installer.` Here is the Install method that we need to put in the Installer Class. Here is most important part—the Install method we need to add. It overrides the default Install method, and runs the important system commands to NGEN. `MSI setup.` Here are the steps, from start to finish, you must take to make the installer class do its magic. It is clearly possible in other installers, but the instructions are different and I have no experience in that area.`Getting started.` Make a setup project. Go to File -> New Project, and make an MSI installation project. Add output files. Add the C# project output files under the Install custom action. `This is accomplished through the VS GUI and just takes some exploration and trial and error.`Visual Studio `visual-studio`Run the new setup.exe created.` Use Process Explorer to verify that no methods are JITted. Process Explorer has a JIT column that you can add, and if that number is zero when you run your program, the method worked. `Process Explorer ``Results.` My programs with NGEN take about half as much time to start up, and use fewer cycles and a bit less memory. NGEN generates native images for an assembly exe, meaning that the JIT compiler doesn't need to run at startup. `The test program was precompiled with NGEN. It slightly improved startup time.`Summary.` We developed a NGEN installer class in the C# language. NGEN images are essentially native programs, and often about as fast as native programs, at least when starting up. The .NET Framework is still required.

XYZEY command lineZ Z[pathYNGEN]Zngen.exe install Z[pathYassembly]Zassembly.exeZNGEN installer class: C#Z YY.CYs; YY.ComponentModel; YY.Configuration.Install; YYY.Runtime.InteropServices; YY.IO;Z /Y<Y> /YThis is run as a custom action by an MSI installer. /YIt is usedYcall NGEN, which optimizes the assembly. /Y</Y> Z[RunInstaller(true)] Ypartial YZNgenInstallerZ : ZInstallerZ { XYNgenInstaller() X{ XXInitializeComponent(); X}Z X/Y<Y> X/YAn override function that is called when a MSI installer is run X/Yand the executable is set as a custom actionYbe run at install time. X/YThe native image generator (NGEN.exe) is called, which provides a X/Ystartup perYmance boost on Windows Forms programs. It is helpful to X/Ycomment out the NGEN codeYsee how the perYmance X/Yis affected. X/Y</Y> X/Y<param name="stateSaver">No needYchange this.</param> XZYoverride YZInstallZ(IDY stateSaver) X{ XXbase.Install(stateSaver);Z XXYget the .NET runtime Y,Yadd the ngen exe at the end. XXZY runtimeStrYRuntimeEnvironment.GetRuntimeDirectory(); XXY ngenStrYPath.Combine(runtimeStr, "ngen.exe");Z XXYcY a Yprocess... XXZProcess processYYProcess(); XXprocess.YInfo.FileNameYngenStr;Z XXYget the assembly (exe) pathYfilename. XXZY assemblyPathYContext.Parameters["assemblypath"];Z XXYadd the argumentYthe filename as the assembly path. XXYUse quotes--important if there are spacesYthe name. XXYUse the "install" verbYngen.exe will compile all deps. XXZprocess.ZYInfo.ArgumentsZY"install \""YassemblyPathY"\"";Z XXYstart ngen. it will do its magic. XXZprocess.ZYZ(); XXprocess.ZWaitForExitZ(); X} }ZNGEN timings beYeYafter: Windows FormsZ BeYe, no precompilation: Z0.2556 secondsZ After, NGEN:XXX Z0.2184 secondsZ [faster]Z