Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to preload .NET assembly

Tags:

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.

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?

Thanks in advance.

like image 253
Dyppl Avatar asked Feb 04 '11 07:02

Dyppl


1 Answers

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

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.

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

private static void LoadSomeStuff(object state) {     GC.KeepAlive(typeof(SomeTypeFromAnotherAssembly)); } ... ThreadPool.QueueUserWorkItem(LoadSomeStuff); 
like image 185
Marc Gravell Avatar answered Sep 23 '22 05:09

Marc Gravell