简体   繁体   English

预加载装配

[英]Preloading Assemblies

At work we use DevExpress for the user interface. 在工作中,我们使用DevExpress作为用户界面。 The first time a form employing a DevExpress control is opened there's a long pause (sometimes 15-20 sec on some clients). 第一次打开使用DevExpress控件的表单会有很长的停顿时间(某些客户端有时会持续15-20秒)。 In Visual Studio i can see that tons of assemblies are being loaded during that phase. 在Visual Studio中,我可以看到在该阶段正在加载大量的程序集。 Is there a way to preload that assemblies into the AppDomain in the background on a thread that is spawned for example before the login screen pops up? 有没有办法在生成的线程的后台将该程序集预加载到AppDomain中,例如在登录屏幕弹出之前?

Another choice is to force the JIT to load the assemblies asynchronious instead of doing it by hand. 另一种选择是强制JIT以异步方式加载程序集而不是手动执行。 The trick is to simply call the constructor of the control, so the Jit knows that it has to start compiling that particular code path. 诀窍是简单地调用控件的构造函数,因此Jit知道它必须开始编译该特定的代码路径。 Usually that forces it to load all dependant assemblies. 通常会强制它加载所有依赖程序集。 Just make sure to surround the call of the constructor by a try catch. 只需确保通过try catch包围构造函数的调用。

An example of how to do that at loadtime: 如何在加载时执行此操作的示例:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        PreJitControls();

        Application.Run(new Form1());
    }

    private static void PreJitControls()
    {           
        ThreadPool.QueueUserWorkItem((t) =>
        {
            Thread.Sleep(1000); // Or whatever reasonable amount of time
            try
            {
                AssemblyPullingControl1 c = new AssemblyPullingControl1();
            }
            catch (Exception) { }

            try
            {
                AssemblyPullingControl2 c = new AssemblyPullingControl2();
            }
            catch (Exception) { }

        });
    }
}

But you could also do something similar in the constructor of the login form, if that is a better time to do the pre-loading. 但是,您也可以在登录表单的构造函数中执行类似的操作,如果这是进行预加载的更好时间。 Just move the PreJitControls method to the login form and call it from the constructor. 只需将PreJitControls方法移动到登录表单并从构造函数中调用它。

This will however force your users to always take that hit on start up. 但是,这会强制您的用户始终在启动时获得该命中。

In general this is a bad idea (if you have the hit at least defer it till you really need it). 一般来说,这是一个坏主意(如果你有至少推迟它直到你真的需要它)。 A case where it might help is to trigger the load if there is a strong chance that they are going to use the functionality in the near future but the system is otherwise idle. 它可以帮助一个例子是触发负载如果有一个很大的机会,他们在不久的将来使用的功能,但系统处于空闲状态。 This can be very hard to do accurately though. 但这可能很难准确。

You might see whether any of the loaded assemblies are under your control and in the GAC. 您可能会看到是否有任何已加载的程序集在您的控制之下和GAC中。 If so you could ngen them which may have a significant effect on the start up time of this aspect of your UI. 如果是这样,你可能会对它们产生影响,这可能会对UI这方面的启动时间产生重大影响。

I'm not sure, but I am guessing that not the actual loading of the assemblies is the timeconsuming part - but probably the JIT compiling of the code path. 我不确定,但我猜测不是组件的实际加载是耗时的部分 - 但可能是代码路径的JIT编译。 Maybe you want to look at ngen. 也许你想看看ngen。 could be that it makes the performance problem go away. 可能会导致性能问题消失。 but be sure to understand the implications of that tool. 但一定要了解该工具的含义。

Links: - http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx - http://msdn.microsoft.com/en-us/magazine/cc163610.aspx 链接: - http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx - http://msdn.microsoft.com/en-us/magazine/cc163610.aspx

have a look at the Assembly.Load methods. 看看Assembly.Load方法。

Gero 格罗

If you are trying to get your assemblies faster, why not have a look at NGEN for your code. 如果您想让组件更快,为什么不看看NGEN代码。 Pre JIT everything in the background. 预JIT后台的一切。 This has pro's and con's though depending on what your app is doing. 虽然取决于你的应用程序在做什么,但这有赞成和反对意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM