在工作中,我们使用 DevExpress 作为用户界面。第一次打开使用 DevExpress 控件的表单时会出现长时间的停顿(在某些客户端上有时会停顿 15-20 秒)。在 Visual Studio 中,我可以看到在该阶段正在加载大量程序集。有没有办法在登录屏幕弹出之前生成的线程的后台将程序集预加载到 AppDomain 中?
最佳答案
另一种选择是强制 JIT 异步加载程序集,而不是手动加载。诀窍是简单地调用控件的构造函数,因此 Jit 知道它必须开始编译该特定代码路径。通常这会强制它加载所有依赖程序集。只需确保用 try catch 包围构造函数的调用即可。
如何在加载时执行此操作的示例:
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) { }
});
}
}
但是您也可以在登录表单的构造函数中执行类似的操作,如果这是进行预加载的更好时机。只需将 PreJitControls 方法移动到登录表单并从构造函数中调用它。
关于c# - 预加载程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/548915/