预加载所有程序集

预加载所有程序集

本文介绍了预加载所有程序集 (JIT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在第一次加载一些繁重的 UI 屏幕时受到了打击.我们的项目分为一个主要的可执行文件和几个 DLL 文件.DLL 文件还可能包含首次加载时速度较慢的 UI 屏幕.

We are taking a hit the first time some heavy UI screens are loaded. Our project is divided into one main executable and several DLL files. The DLL files can also contain UI screens which are slow the first time they are loaded.

有没有办法(在代码中)我们可以预加载所有引用的程序集以避免 JIT 编译命中?

Is there a way (in code) we can preload all the referenced assemblies so as to avoid the JIT compilation hit?

我知道有一种工具叫做 NGen.是否可以在开发环境中运行 NGen 以便我们立即看到其效果?但理想情况下,我们希望从代码中预加载引用的程序集.

I know there is a tool called NGen. Is it possible to operate NGen in a development environment so we can see its effects instantly? Ideally though, we would like to preload the referenced assemblies from code.

为我们的 UI 组件使用 C# .NET 3.5 和 DevExpress.

Using C# .NET 3.5 along with DevExpress for our UI components.

推荐答案

我个人发现将预抖动放在程序"中对某些应用程序有帮助,但这是一个特定于情况的问题.

I personally found that putting the pre-jitter in 'Program' helped in a certain application, but that is a situation-specific question.

更重要的是,wal's answer中的代码遇到会崩溃一个抽象方法,所以添加了两行代码来跳过抽象方法.

More importantly, the code in wal's answer will crash when it encounters an abstract method, so two lines of code have been added to skip abstract methods.

static Program()
{
    foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
    {
        foreach (var method in type.GetMethods(BindingFlags.DeclaredOnly |
                            BindingFlags.NonPublic |
                            BindingFlags.Public | BindingFlags.Instance |
                            BindingFlags.Static))
        {
            if ((method.Attributes & MethodAttributes.Abstract) == MethodAttributes.Abstract|| method.ContainsGenericParameters)
            {
                continue;
            }
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle);
        }
    }
    Console.WriteLine("jitted!");
}

这篇关于预加载所有程序集 (JIT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:07