?
Solved

dynamic loaded dll question

Posted on 2009-07-04
10
Medium Priority
?
216 Views
Last Modified: 2012-05-07
hi all,

each time I'm loading a new dll (containing a winform)  dynamically to my main app, the previously loaded one (if there was one) isn't unloaded. the dlls are loaded dynamically when I select a button and their winforms are automatically shown. when I select another button, there is another dll loaded and another winform shown, the last should be closed. when I do this several times, the application freezes.

why is this? should I somehow unload the dll before loading a new one?

hope I was clear enough :)
thanks

d.
0
Comment
Question by:ingprokop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 

Author Comment

by:ingprokop
ID: 24777530
would be nice if there was a possibility to load all the dlls with the winforms at the beginning and then just use the instances of the forms instead of loading the dlls with the forms each time I press that button...
0
 
LVL 19

Expert Comment

by:daveamour
ID: 24779085
You can load them all at the beginning and having many open shouldn't be a problem - there must be a bug in your code somehwere.
You can load them all and put them in a collection  - eg a Dictionary
0
 

Author Comment

by:ingprokop
ID: 24779670
yes, that's what I'm looking for. I want to load them one after another form a folder and then just create and hide instances of the forms they contain. I never worked with a dictionary. So I can put the whole object into a dictionary like using an array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
daveamour earned 2000 total points
ID: 24782662
Yes a dictionary is a collection of things.
Each thing in the dictionary consists of 2 objects.  One object would be something you use to lookup the thing - eg a dll name and the other object would be an instance of your dll.
Remember the very first code I showed you for this topic - that did it this way - see attached code again.
Noe that the Dictionary needs you to specify what type of objects you will use.  String fro your first one and use the common interface for the second one as all of your dlls will be of that interface type.
Also see:
http://www.vcskicks.com/dictionary.php
 

        private void LoadInfoHandlers()
        {
            infoCardFactories = new Dictionary<string, IInfoCardFactory>();
 
            // Get list of all dll files in same folder as main executable and
            // iterate through them seeing if any of them contain classes that
            // implement IInfoCardFactory.  
            string[] dllFiles = System.IO.Directory.GetFiles(".", "*.dll");
            foreach (string file in dllFiles)
            {
                Assembly assembly = Assembly.LoadFrom(file);
 
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass && type.GetInterface("IInfoCardFactory") != null)
                    {
                        // IInfoCardFactory is implemented. Create a new instance of the object
                        // and determine what categories are supported. Build the list of categories
                        // and the IInfoCardFactory that creates objects of that category type
                        
                        IInfoCardFactory infoCardFactory = (IInfoCardFactory)Activator.CreateInstance(type);
                        //IInfoCardFactory infoCardFactory = (type)Activator.CreateInstance(type);
 
 
                        string[] categoriesSupported = infoCardFactory.CategoriesSupported;
                        foreach (string category in categoriesSupported)
                        {
                            infoCardFactories.Add(category, infoCardFactory);
                        }
                    }
                }
            }
        }

Open in new window

0
 

Author Comment

by:ingprokop
ID: 24783716
aha, that means I load all the dlls from the folder, will store them one by one into the dictionary and then, when needed create an instance of the assembly from which I create an instance of the contained form...

will check it right now...

thanks a lot
0
 
LVL 19

Expert Comment

by:daveamour
ID: 24783723
Yep thats right but you can create the instances all at once and store the instances in the dictionary if you like.
0
 

Author Comment

by:ingprokop
ID: 24783738
so I can create the instances of the forms from the assemblies and store them into the dictionary, I actually don't need the assemblies anymore. will try that, great
0
 

Author Comment

by:ingprokop
ID: 24785909
before, each time a button was clicked, an assembly was loaded and the interface event was "linked to it". I can now "link" (what is the correct word for it ... ) all of the interfaces for all the forms in the dictionary on the main form load, right? then, when sending the refresh event I just choose what form in the dictionary to send it to.

I had the problem that when sending the refresh event, the data was sent to all of the instances of the forms - that was sometimes quite slow because of large strings and the program just hung up.
0
 

Author Comment

by:ingprokop
ID: 24786264
great, it works and is much faster... will test it with larger strings and we'll see. will let you know...
0
 

Author Comment

by:ingprokop
ID: 24786282
...actually I have to relink the events each time I open the form instance - is actually quite logic
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question