Results 1 to 3 of 3

Thread: Unloading resources loaded at run-time

  1. #1

    Thread Starter
    Addicted Member Vitani's Avatar
    Join Date
    Jul 2001
    Location
    England
    Posts
    134

    Unloading resources loaded at run-time

    Hi,

    I've got a program that uses plugin DLLs. At the moment they load fine, and work wonderfully. However the whole reason for using plugins is that you can work on one, make changes, and reload it while the main application is still running.

    My problem is when I unload the plugins, the accociated dlls are still "locked" to the program, and can't be deleted or overwritten.

    Here's how I'm doing it:

    PHP Code:
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;

    .
    .
    .

        public 
    void load_plugins() {

          
    Interfaces.Plugin plugin;
          
    Type[] ObjType null;
          
    DirectoryInfo dir = new DirectoryInfo("plugins");
          
    FileInfo[] arrFilename dir.GetFiles("*.dll");
          
    string filename "";

          foreach (
    FileInfo f in arrFilename) {

            
    filename "plugins\\\\" f.Name;

            try  {
              
    // load a single dll
              
    Assembly asm null;
              
    asm Assembly.LoadFrom(filename);
              if (
    asm != null) {
                
    // create an array of types
                
    ObjType asm.GetTypes();
              }

            } catch (
    Exception ex) {
              
    MessageBox.Show("An error occured while trying to load '" filename "': " ex.Message);
            }

            try  {
              
    // We need to find all classes that implement the Plugin interface
              
    if (ObjType != null) {
                
    // lets loop through each type in our array of types in the loaded dll
                
    foreach(Type pluginImplemented in ObjType) {

                  
    // If the plugin interface is bound to one of the types, then that type
                  // is a class that implements Interfaces.Plugin.
                  
    if(pluginImplemented.GetInterface("Interfaces.Plugin") != null) {

                    
    plugin = (Interfaces.Plugin)Activator.CreateInstance(pluginImplemented);

                    if (
    plugin.Initialize(CarrotzBotdbCon)) {
                      
    CarrotzBot.plugins.Add(plugin);

                    } else {
                      
    MessageBox.Show("Failed to Initialize: " plugin.Name());

                    } 
    // end of initilize check

                  
    // end of is plugin check

                
    // end of foreach type loop

              
    // end of ObjType check

            
    } catch (Exception ex) {
              
    MessageBox.Show("An error occured while trying to initialize '" filename "': " ex.Message);

            } 
    // end of error checking

          
    // end of foreach file loop

          
    this.btnPlugins.Text "Unload Plugins";

        } 
    // end of load_plugins()

        
    public void unload_plugins() {
          
    Interfaces.Plugin plugin;
          
    int i;
          
    int plugins CarrotzBot.plugins.Count;
          
          for (
    0pluginsi++) {
            
    plugin CarrotzBot.plugins[0];
            
    unload_plugin(plugin);
            
          }
          
        }

        public 
    void unload_plugin(Interfaces.Plugin plugin) {

          
    plugin.Shutdown();
          
    CarrotzBot.plugins.Remove(plugin);

          if (
    CarrotzBot.plugins.Count == 0)
            
    this.btnPlugins.Text "Load Plugins";
          
        } 
    load_plugins() work fine, and loads the plugins into the program, and they can be used/called sucessfully.
    unload_plugins() is a quick way to unload all the plugins that are loaded, this works in that it calls unload_plugin() for each loaded plugin
    unload_plugin() shuts down the plugin, and removes it from the collection, however the dll accosiated with it is still open by the application, and can't be deleted/mopved/overwritten. However it can be renamed.

    So my question is, how do I unload a plugin, loaded in runtime, so that it releases the dll it lives in?



    [Edit: Oooh, just realised, this is my 100th post! Rah!! And it's only taken me 2 years!!]
    Last edited by Vitani; Jul 2nd, 2003 at 06:50 AM.
    If you can dream it, you can do it - Moo Power!

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Sent you a PM.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm i'd like to know to! can u say it cander plz?
    \m/\m/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width