Help regarding assemblies

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

Hi,

I would like to be able to load an assembly file at runtime which will do some work. When the work is done
I would like the main application to be informed so that the assembly can be unloaded. How can I do this using
delegates and events?
 
Hi,


You load the assembly using Assembly.Load*

AFAIK there is no method to unload an assembly. Why you want to do this?

cheers,
 
I didn't really mean to say unload assembly but rather just unload the whole application domain. You see I got this library that
opens Outlook.exe and does some work, and when the user closes Outlook, the library marshals all the objects and shutsdown the
server (outlook.exe). I would also like to unload the whole application domain as well just to make sure everything is killed, so I
would have something like this:

System.AppDomain newDomain = System.AppDomain.CreateDomain("OutlookApplicationDomain");
newDomain.ExecuteAssembly(@"c:\OpenOutlook.exe");

But since outlook is controlled by the user I need some event to get back to the main application before I unload the domain:

System.AppDomain.Unload(newDomain);

Any idea how to do this?

Cheers,
Max
 
Max said:
Hi,

I would like to be able to load an assembly file at runtime which will
do some work. When the work is done
I would like the main application to be informed so that the assembly can
be unloaded. How can I do this using
delegates and events?

You can't unload assemblies, you can only unload application domains (AD),
however, you can't unload the default AD, so you'll need to create an extra
AD, load your assembly into that domain, create an instance of a type and
call a method on that type. When the method returns you can unload the AD to
get rid of the AD and the assemblies loaded into that AD.
Check MSDN for info and samples, keywords are CreateDomain,
CreateInstanceFromAndUnwrap, Unload....

Willy.
 
When you call ExecuteAssembly, the call returns when your OpenOutlook.exe
Main function returns, that is when you have done in your Main function, so
there is no need for an event, just unload the AD when the call returns.
Don't forget that your OpenOutlook.exe Main function runs on the callers
thread, so you probably want to create an auxiliary thread to run this code,
and because you are using COM interop in that assembly you'll have to
initialize the thread to enter a STA before starting.

Thread t = new...
t.ApartmentState= ApartmentState.STA;
t.Start();

Note also that you might want to change the assembly type from an exe into a
DLL and use CreateInstanceAndUnwrap to load a type in an auxiliary AD.

Willy.
 
Back
Top