在工作中,我们使用 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 方法移至登录表单并从构造函数中调用它即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)