一篇文章解密几个提升WPF应用程序冷启动性能的方法!(Part 1)

在本文中,将为大家记录9个提升WPF应用程序冷启动性能的策略。

本文记录9个提升WPF应用程序冷启动性能的策略,面列出的3个技术适用于所有WPF应用程序,无论使用什么组件:

  • Ngen.exe
  • MultiCore JIT
  • ReadyToRun

以下技术是针对

DevExpress技术交流群7:674691612      欢迎一起进群讨论

1. Ngen.exe

Native Image Generator (Ngen.exe) (原生图像生成器)是优化 .NET框架应用程序((.NET/. NET Core项目不支持Ngen)应该考虑的第一个工具,.NET框架项目使用Microsoft中间语言(MSIL)代码生成程序集。在执行应用程序之前,需要将此代码转换为机器代码,从MSIL到机器代码的转换在启动时就开始了——这个过程可能需要大量的时间。

开发人员可以使用Ngen.exe生成已经包含本机代码的原生映像库,需要注意的是,Ngen.exe应该在将要使用应用程序的机器上使用。开发人员可以在自己的机器上运行它来测试性能,但为了优化终端用户的冷启动,需要在用户机器上使用Ngen.exe。

在用户的机器上运行Ngen的最佳方法是将Ngen.exe合并到应用程序安装程序中。在安装过程中,开发人员需要执行以下命令行,Ngen.exe将自动处理与项目相关的所有程序集:

C:WindowsMicrosoft.NETFrameworkv4.0.30319ngen.exe install C:MyApp.exe

如果开发者将应用程序作为单击一次或存档(并且没有安装程序)分发,则可以在应用程序启动期间从代码中调用Ngen.exe。要在第一次启动时只运行Ngen.exe,为可执行文件计算一个哈希值,并在后续启动时检查这个哈希值:

var savedHash = string.Empty;var assemblyLocation = Assembly.GetEntryAssembly().Location;// Specify a path to the file that stores your executable’s hash.// Create this file or load the saved hash if the file already exists:var hashPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hash.txt");if (!File.Exists(hashPath)) {File.Create(hashPath);} else {savedHash = File.ReadAllText(hashPath);}// Obtain the hash for your executable.// Cancel the operation if the application does not have changes:var hash = string.Concat(SHA1.Create().ComputeHash(File.ReadAllBytes(assemblyLocation)).Select(x => x.ToString("x2")));if (hash.Equals(savedHash))return;// Obtain the path to ngen.exe:var dotNetRuntimePath = RuntimeEnvironment.GetRuntimeDirectory();var ngenPath = Path.Combine(dotNetRuntimePath, "ngen.exe");// Create a process that runs ngen.exe:var process = new Process {StartInfo = new ProcessStartInfo {FileName = ngenPath,// Pass the path to your executable:Arguments = $"install "{assemblyLocation}"" /nologo""

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2023年1月3日
下一篇 2023年1月3日

相关推荐

发表回复

登录后才能评论