Пример #1
0
        //This attempts to prevent the program from crashing if a plugin throws an exception.
        //It is a rather dirty approach and repeated calls will cause a memory leak.
        private void PreventPluginCrash(object sender, UnhandledExceptionEventArgs e)
        {
            StackTrace trace = new StackTrace((Exception)e.ExceptionObject);

            foreach (StackFrame frame in trace.GetFrames())
            {
                Assembly frameAssembly = frame.GetMethod().DeclaringType.Assembly;
                if (Plugins.ContainsKey(frameAssembly.GetName().Name))
                {
                    IMSPluginBase plugin = Plugins[frameAssembly.GetName().Name];
                    Logger.WriteError("An uncaught exception was raised in plugin " + plugin.Name + "!  The plugin has been disabled.  Error:\n" + e.ExceptionObject);
                    try
                    {
                        plugin.Stop();
                    }
                    catch (Exception stopException)
                    {
                        Logger.WriteError("During fatal error unload, the plugin's stop method also failed.  Exception:\n" + stopException);
                    }
                    Plugins.Remove(plugin.Name);
                    KnownPlugins[plugin.PluginAssembly.GetName().Name].Enabled = false;
                    Thread.CurrentThread.IsBackground = true;
                    Thread.CurrentThread.Join(); //suspend the thread indefinitely to stop the CLR from shutting down
                }
            }
            Logger.WriteError("A fatal error occured, resulting in IMS shutting down!\n" + e.ExceptionObject);
        }
Пример #2
0
 /// <summary>
 /// Attempts to remove a plugin from memory.  This does not guarantee that the plugin's assembly will be unloaded.
 /// </summary>
 /// <param name="plugin">The plugin to unload.</param>
 public void UnloadPlugin(IMSPluginBase plugin)
 {
     lock (Locker)
         if (Plugins.ContainsKey(plugin.PluginAssemblyName))
         {
             plugin.Stop();
             Plugins.Remove(plugin.PluginAssemblyName);
         }
         else
         {
             throw new ArgumentException("This plugin object has not been loaded by the plugin manager.");
         }
 }