gpt4 book ai didi

c# - 首次运行时自动运行 Ngen.exe

转载 作者:行者123 更新时间:2023-11-30 14:32:38 26 4
gpt4 key购买 nike

我的应用没有安装程序。它是可移植的,但我需要在其上运行 ngen.exe,因为它在启动时运行。

是否建议在应用程序首次运行时自动运行 ngen.exe?以后会不会出问题?有内置的方法吗?

最佳答案

是否建议在应用首次运行时自动运行 ngen.exe?

我从未读过或听过任何此类建议,但这是一个有趣的想法。

我说去做吧,测试它是否适合您的情况(就您的“便携”/“无安装程序”要求而言,这很有趣)。

有内置的方法吗?

在第一次运行应用程序时,没有内置的方式来运行 NGen;但它可以按照下面的 PoC 演示来完成。

PoC代码

以下 PoC 代码包含 code from a related SO 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 the app has been NGen'd with code adapted from
* https://stackoverflow.com/a/20593260/1810429, which also outlines
* an alternative approach - by running...
* ngen.exe display <assemblyPath>
* ...and checking the result - 0 if the app is already NGen'd and
* -1 if it is not.
*/

Process process = Process.GetCurrentProcess();

ProcessModule[] modules = new ProcessModule[process.Modules.Count];
process.Modules.CopyTo(modules, 0);

var niQuery =
from m in modules
where m.FileName.Contains(@"\" + process.ProcessName + ".ni")
select m.FileName;
bool ni = niQuery.Count() > 0 ? true : false;

// FORNOW: for PoC debugging and sanity checking
if (ni)
Console.WriteLine("Native Image: " + niQuery.ElementAt(0));
else
Console.WriteLine("IL Image: " + process.MainModule.FileName);

/*
* NGen the app if not.
*/

if (!ni)
{
// FORNOW: for PoC debugging and sanity checking
Console.WriteLine("The app is not NGen'd.");
Console.WriteLine("NGen'ing the app...");

var assemblyPath = process.MainModule.FileName;

ProcessStartInfo startInfo = new ProcessStartInfo();
// TODO: Determine the path to (the appropriate version of)
// ngen.exe.
// FORNOW: Just use a hardcoded path to ngen.exe for PoC.
startInfo.FileName =
@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe";
startInfo.Arguments = "install \"" + assemblyPath + "\"";
// TBD: process options that you 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 that you think makes sense - e.g.
// logging or displaying the error, moving on regardless
// etcetera.
}
}
else
{
// FORNOW: for PoC debugging and sanity checking
Console.WriteLine("The app is already NGen'd.");
}

/*
* Carry on with whatever your app does.
*/
}
}
}

第一次运行

C:\bin\SelfNgenPoC>.\SelfNgenPoC.exe
IL Image: C:\bin\SelfNgenPoC.exe
The app is not NGen'd.
NGen'ing the app...
Microsoft (R) CLR Native Image Generator - Version 4.0.30319.18408
Copyright (c) Microsoft Corporation. All rights reserved.
1> Compiling assembly C:\bin\SelfNgenPoC.exe (CLR v4.0.30319) ...

C:\bin\SelfNgenPoC>

后续运行

C:\bin\SelfNgenPoC>.\SelfNgenPoC.exe
Native Image: C:\Windows\assembly\NativeImages_v4.0.30319_32\SelfNgenPoC\a461633
0444188e116025424c70d15f1\SelfNgenPoC.ni.exe
The app is already NGen'd.

C:\SelfNgenPoC>

以后会不会有什么问题?

程序集对 NGen 是否有意义取决于许多因素,您可以在 an MSDN blog post 中查看这些因素, NGen documentation on MSDN , an older but relevant "MSDN Magazine" article , 以及其他地方。

最终只有您充分了解您的应用,才能确定它对 NGen(任何、部分或全部)程序集是否有意义。

假设它确实在您的情况下有意义,我不认为它会根据您所描述的内容导致任何明显的问题。

请务必牢记标准的 NGen 职责 - 特别是 NGen MSDN 文档中提到的这一职责...

Images need to be regenerated when the original assembly or one of its dependencies is serviced.

...这应该可以通过更高级的自生成逻辑进行管理。

关于c# - 首次运行时自动运行 Ngen.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com