views:

78

answers:

1

Lets say i load Form1 and then it shows buttons which let me access Form 2,3 and 4.

Now, when i click the button to load Form2, I can see that it takes a bit of time to load a few class libraries (.dll) which makes the UI look unresponsive.

I don't see the form until the class libraries are loaded which makes the Form1 to still be shown until the libraries are loaded. But after that, it is pretty fast.

So how do i pre-load the class libraries, perhaps when the application is started?

I tried putting the using statements to the Form1 and the compact framework is so good that it wont load until it is actually required by Form2. I am talking about 2 seconds of delay but still looks bad.

Is there anyway to overcome this problem? Cant i force the CF to load my dll's file it is loading System.dll, System.Windows.Forms.dll etc?

UPDATE:

I could load my own class libraries using Assembly.LoadFrom but I am still unable to load the following files when Form1 loads

Microsoft.WindowsMobile.PocketOutlook.dll
Microsoft.WindowsCE.Forms.dll
Microsoft.WindowsMobile.Utilities.dll
System.Xml.dll

Is it even possible? :)

+2  A: 

It's not about "loading a class library" it's about what code needs to be JITted (runtime compiled from IL to machine code). THe first time you show your form, the runtime has to JIT all of the code to create your class instances. It also has to run the constructor, etc. Subsequent times, it may only be showing the form, and even if it has to load a new instance, the JITted code may already be cached and ready to run, meaning no compile is necessary.

To get the best "visible" performance, you want to keep the amount of JITting necessary when the user click a button to a minimum. If your Form is going to need some services or objects, pre-load them in a background thread so they are ready when the Form needs them.

Without knowing a bit more about exactly what you're loading, when you're loading it, and what your Form's actually need to display, it's difficult to give you more detailed direction.

Edit

It might look something like this:

class MyForm
{
    static void Main()
    {
        new Thread(delegate
            {
                AppInitialize();
            })
            {
                IsBackground = true
            }
            .Start();

        Application.Run(new MyForm());
    }

    static void AppInitialize()
    {
       // load app-wide resources, services, etc
    }

    public MyForm()
    {
        InitializeComponent();

        ThreadPool.QueueUserWorkItem(
            delegate
            {
                InitializeServices();
            });
    }

    void InitializeServices()
    {
        // load up stuff the Form will need after loading/first rendering
    }
}

Again, since I have no idea what you're doing, it may need to be different for your application. I also use an IoC container framework, so my code looks a lot different. Fundamentally I use the same concepts, though.

ctacke
@ctacke: Thanx but my actual problem is HOW to do that? How do i load the dll's mentioned in my question in a background thread? :)
Ranhiru Cooray
You *don't* load DLLs. You have the loader load *class instances*.
ctacke
The problem is that all UI related stuff cannot be preloaded in a background thread because of thread affinity of UI controls.
Juanma
"UI related" is pretty broad. Creating the actual controls is not going to take that long, it's usually data fetching, etc. that takes time.
ctacke