My app doesn't have an installer. It is portable but I need to run ngen.exe
on it because it runs on startup.
Is it recommended to autorun ngen.exe
on the first run of the app? Will it cause any problems later? Is there a built in way to do it?
If you're using dotNEt > 4.5
public App(){
ProfileOptimization.SetProfileRoot(@"C:\MyApp");
ProfileOptimization.StartProfile("Start.Profile");
}
Put this on the very start of your programm. The profile is created the first time your app is starting/profile is missing. MSDN
Is it recommended to autorun ngen.exe on the first run of the app?
I have never read or heard any such recommendation, but it is an interesting idea.
I say go for it and test whether it works for your situation (which is interesting insofar as your "portable"/"no-installer" requirement).
Is there a built in way to do it?
There is not a built-in way to run NGen on the first run of an app; but it can be done as the PoC below demonstrates.
PoC code
The following PoC code incorporates code from a related SO answer.
first run
subsequent run
Will it cause any problems later?
Whether it makes sense to NGen an assembly depends on many factors, which you can review in an MSDN blog post, NGen documentation on MSDN, an older but relevant "MSDN Magazine" article, and elsewhere.
Ultimately only you know enough about your app to determine whether it makes sense to NGen (any, some, or all of) its assemblies.
Assuming it does make sense in your situation, I don't expect it will cause any distinct problems based on what you have described.
Just be sure to keep standard NGen responsibilities in mind - particularly this one noted in the NGen MSDN documentation...
...which should be manageable with a little fancier self-NGen'ing logic.