预加载.NET程序集的好方法 [英] Good way to preload .NET assembly

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

问题描述

在我的应用程序,我需要显示在鼠标点击的形式。问题是,形式是在另一个组装和装配,因为装载的懒惰本性的很可能是按下鼠标按钮时,该组件没有载入。所以我有很明显的暂停的形式似乎终于之前。

In my app I need to show a form on mouse click. The problem is that the form is in another assembly and because of lazy nature of assembly loading it is likely that the assembly isn't loaded yet when the mouse button is pressed. So what I have is very noticeable pause before the form finally appears.

我能够通过调用拿出哑修复新FormFromAnotherAssembly()在我的初始化方法。那当然,拿了东西照顾和暂停不再存在,但它是非常难看。我喜欢这个解决方案的唯一的事情是,我没有与路径和程序集名称这是我必须做的,如果我想使用类似的Assembly.Load 一塌糊涂

I was able to come up with a dumb fix by calling new FormFromAnotherAssembly() in my initialization method. That, of course, took care of things and the pause is no longer there, but it's very ugly. The only thing I like about this solution is that I don't have to mess with paths and assembly names which I have to do if I want to use something like Assembly.Load.

那么,什么是选择的好,强大的解决方案,如果我想确认之前,我确实需要的组件装?

So, what's the good, robust solution of choice if I want to make sure the assembly is loaded before I actually need it?

先谢谢了。

推荐答案

在你的初始化明确预负荷可能仍然是最好的选择。

Explicit pre-load in your init is probably still your best option.

typeof运算(SomeTypeFromAnotherAssembly)应该是足够了 - 伴随着不能被优化掉一些不透明的方法;或许是:

a typeof(SomeTypeFromAnotherAssembly) should be enough - along with some opaque method that can't be optimised away; perhaps:

GC.KeepAlive(typeof(SomeTypeFromAnotherAssembly));

这避免了。需要注意的是,这将是的加载的,但不是即时编译等等

This avoids the new. Note that this will be loaded, but not JITted etc.

如果你愿意,你可以做一个BG螺纹:

If you wanted, you could do it on a BG thread:

private static void LoadSomeStuff(object state) {
    GC.KeepAlive(typeof(SomeTypeFromAnotherAssembly));
}
...
ThreadPool.QueueUserWorkItem(LoadSomeStuff);

这篇关于预加载.NET程序集的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆