Solved

unload a dll assembly

Posted on 2009-06-30
15
384 Views
Last Modified: 2012-05-07
hi all,

I have a problem when loading the same assembly twice (it's an assembly containing a form). when loading I'm sending some variables to the new assemblyform to make it ready for my needs. when I close the assemblyform and want to open it again, the refresh event sent to this assembly won't work anymore. do I need to unload the assembly and load it again? how do I do it?

thanks in advance

d.
0
Comment
Question by:ingprokop
  • 8
  • 7
15 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24744054
I'd be very interested to see how you execute some code during the assembly load, as unless you use the LoadEvent from the domain manager, you cannot do that without having a mixed assembly and compiling it in C++ and even then, it is very tricky to get right...

Also, an assembly, once loaded, cannot be unloaded.

But I think you mean something else. Correct me if I am wrong, but are you trying to load a second instance of your form after closing the original instance? If not, can you elaborate a bit on your requirement?
0
 

Author Comment

by:ingprokop
ID: 24744078
this is code I'm using to load the assembly and create an instance of it's form

Assembly assembly = Assembly.LoadFile(Directory + "assemblyform.dll");

Form form = (Form)assembly.CreateInstance(className);

then I'm sending an event with some variables to update the controls on the assembyform. works only once.

when I need to open the form again, I use the same procedure... open the assembly, create an instance of the form etc.
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24744140
Loading an assembly twice does not have the effect of loading the assembly twice. You will only need to load the assembly once. Once loaded, it is loaded and it stays loaded, even if the Assembly object goes out of scope.

What you meant with "when loading an assembly", you actually meant "when instantiating the form". How does the code look that sends the refresh event and how does the code look that does the redrawing upon receiving the event? Are you sure you are sending it to the correct form?

Is it a possible workaround for you to hide the form instead of to close it? Can the refresh event be fired multiple times to the same form? In that case, hiding and showing is probably an easier way to achieve the same goal and much more efficient too.

-- Abel --

PS: if you are hard-coding the assembly name in your code, you can also create a reference to the assembly through References, which makes all the objects from the assembly directly accessible and you do not need to use this cumbersome and slow late binding anymore.
0
 

Author Comment

by:ingprokop
ID: 24744203
I can't hide it, it needs to be closed. imagine a button which loads a form. the user specifies a path and the button1.clicked opens the form and closed the old one opened. everytime the button is clicked I'm loading an assembly the way I wrote before and instantiating the form it contains.

I'm using an interface library to send the events to and from the form. that definitely works. but just once.


Assembly assembly = Assembly.LoadFile(Directory + "assemblyform.dll");

Form form = (Form)assembly.CreateInstance(className);

Refresh += new EventHandler(((Interfaces.Data)form).Refresh);

                    if (Refresh != null)
                    {
                        Refresh(Sender, e); //do send variables
                    }




0
 
LVL 39

Expert Comment

by:abel
ID: 24744313
The code bits on themselves don't give enough context to understand your question. I looked up some of your older questions and found that daveamour has helped you extensively. Do you know of the E-E feature "Ask Related Question", it will give the commenters of your original question an extra email and it will show on top of your new question so that other experts understand where you are coming from.

I downloaded the excellent example from daveamour, which worked almost instantly. Now it is also clear why you use terms like "assemblyform" even though technically there is no such thing as an assembly form vs. a normal form: they are all just forms (perhaps with extra interfaces or methods).

Back to your question: can you update the example from daveamour in such a (minimalistic) way that it examplifies your problem? You don't seem to do anything wrong (apart from using Assembly.LoadFile more often then you need to, but that's not interfering with the correct workings) but without seeing the larger picture or being able to mimic your situation I'm afraid I cannot do much...

If you upload, rename the zip file to *.txt or something, otherwise, EE refuses the upload.
0
 

Author Comment

by:ingprokop
ID: 24744613
I think I got the problem, not the solution yet.

I'm sending an event to the imported form to close the form. when the form is closed I'm not able to access it anymore even if reopened.

open the example by daveamour. open the form and Create Assembly Form by clicking the button. right then, close it. do this 3 times. after then, Create Assembly Form again and Raise Event by clicking the other button. you'll see that for each opened form before raising the event you'll get a message box. if you build the solution again and right after Creating Assembly Form you Raise the Event, everything works fine... until you close the form again.

if there are more than one sort of the form... you can imagine.
0
 

Author Comment

by:ingprokop
ID: 24744633
I made everything like daveamour suggested and everything works perfect, just this issue. somehow when Raising the Event, like in this example, sending the Event can't find the right form to send it to. with the Messagebox you can see it all, I don't use any Messageboxes so I couldn't see the issue properly
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:ingprokop
ID: 24744694
the easiest way I can do it right now is to show and hide the form as you suggested before. will try it and will come back soon.
0
 
LVL 39

Expert Comment

by:abel
ID: 24744716
small Q: why using events? You can also use simple method calls and an interface that is implemented by all forms, right? Will likely be much easier and prevents the binding to the events issues.
0
 

Author Comment

by:ingprokop
ID: 24744739
with hide and show everything works great. thanks a lot.

can you post me an example how to "bind" the methods?
0
 
LVL 39

Expert Comment

by:abel
ID: 24745097
See the form AssemblyForm. It inherits from IPluginAble. You can add any method to IPluginAble and that method must then be implemented by AssemblyForm. From your MainForm, you can simply cast to IPluginAble. Of course, this means you need a shared library CommonInterfaces that you can use, but you already have that.

// get the form

Form form = (Form)assembly.CreateInstance(className);
 

// get the interface

IPluginAble pluginAble = (PluginAble) form;
 

// use the interface

pluginAble.RefreshForm()
 
 

// interface now contains events, but you can add any method:

public interface IPluginAble

{

    event EventHandler ButtonClicked;

    event EventHandler TextHasChanged;
 

    void SomeMethod(object sender, EventArgs e);

    void RefreshForm();    // added a method to interface

}

Open in new window

0
 

Author Comment

by:ingprokop
ID: 24745156
Great, thanks a lot. so it's the same as in daveamour's example... thank you guys, that really helped me...
0
 
LVL 39

Expert Comment

by:abel
ID: 24745412
No, it's not the same. I took his example to make it easier for you to grasp. I added a method. You can forget about the events, unless you have a special reason to use events. I only left them in so you had a reference to where my idea came from and where you should put your new logic and new approach.

After you're done, the interface should look like this:

public interface IPluginAble

{

    void RefreshForm();    // added a method to interface

}

Open in new window

0
 

Author Comment

by:ingprokop
ID: 24745589
I meant the principe is the same
0
 
LVL 39

Expert Comment

by:abel
ID: 24745818
aha, ok, sorry ;-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now