c# - Autorun Ngen.exe on First Run -


my app doesn't have installer. portable need run ngen.exe on because runs on startup.

is recommended autorun ngen.exe on first run of app? cause problems later? there built in way it?

is recommended autorun ngen.exe on first run of app?

i have never read or heard such recommendation, interesting idea.

i go , test whether works situation (which interesting insofar "portable"/"no-installer" requirement).

is there built in way it?

there not built-in way run ngen on first run of app; can done poc below demonstrates.

poc code

the following poc code incorporates code related answer.

using system; using system.diagnostics; using system.io; using system.linq; using system.threading;  namespace selfngenpoc {     class program     {         static void main(string[] args)         {             /*              * check whether app has been ngen'd code adapted              * https://stackoverflow.com/a/20593260/1810429, outlines              * alternative approach - running...              *     ngen.exe display <assemblypath>              * ...and checking result - 0 if app ngen'd ,              * -1 if not.              */              process process = process.getcurrentprocess();              processmodule[] modules = new processmodule[process.modules.count];             process.modules.copyto(modules, 0);              var niquery =                 m in modules                 m.filename.contains(@"\" + process.processname + ".ni")                 select m.filename;             bool ni = niquery.count() > 0 ? true : false;              // fornow: poc debugging , sanity checking             if (ni)                 console.writeline("native image: " + niquery.elementat(0));             else                 console.writeline("il image: " + process.mainmodule.filename);              /*              * ngen app if not.              */              if (!ni)             {                 // fornow: poc debugging , sanity checking                 console.writeline("the app not ngen'd.");                 console.writeline("ngen'ing app...");                  var assemblypath = process.mainmodule.filename;                  processstartinfo startinfo = new processstartinfo();                 // todo: determine path (the appropriate version of)                 // ngen.exe.                 // fornow: use hardcoded path ngen.exe poc.                 startinfo.filename =                     @"c:\windows\microsoft.net\framework\v4.0.30319\ngen.exe";                 startinfo.arguments = "install \"" + assemblypath + "\"";                 // tbd: process options think make sense                 startinfo.createnowindow = false;                 startinfo.useshellexecute = false;                 startinfo.windowstyle = processwindowstyle.hidden;                  try                 {                     using (process exeprocess = process.start(startinfo))                     {                         exeprocess.waitforexit();                     }                 }                 catch                 {                     // tbd: error handling think makes sense - e.g.                     // logging or displaying error, moving on regardless                     // etcetera.                 }             }             else             {                 // fornow: poc debugging , sanity checking                 console.writeline("the app ngen'd.");             }              /*              * carry on whatever app does.              */         }     } } 

first run

c:\bin\selfngenpoc>.\selfngenpoc.exe il image: c:\bin\selfngenpoc.exe app not ngen'd. ngen'ing app... microsoft (r) clr native image generator - version 4.0.30319.18408 copyright (c) microsoft corporation.  rights reserved. 1>    compiling assembly c:\bin\selfngenpoc.exe (clr v4.0.30319) ...  c:\bin\selfngenpoc> 

subsequent run

c:\bin\selfngenpoc>.\selfngenpoc.exe native image: c:\windows\assembly\nativeimages_v4.0.30319_32\selfngenpoc\a461633 0444188e116025424c70d15f1\selfngenpoc.ni.exe app ngen'd.  c:\selfngenpoc> 

will cause problems later?

whether makes sense ngen assembly depends on many factors, can review in an msdn blog post, ngen documentation on msdn, an older relevant "msdn magazine" article, , elsewhere.

ultimately know enough app determine whether makes sense ngen (any, some, or of) assemblies.

assuming does make sense in situation, don't expect cause distinct problems based on have described.

just sure keep standard ngen responsibilities in mind - particularly 1 noted in ngen msdn documentation...

images need regenerated when original assembly or 1 of dependencies serviced.

...which should manageable little fancier self-ngen'ing logic.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -