How to unload plugins?

Hello,

  1. I’m currently disabling the plugin by setting loopratehz = 0.
    I’m wondering if this is sufficient, or if I should completely unload the plugin to free up memory or other resources.

  2. The following code doesn’t seem to work as expected:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MissionPlanner.Utilities;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading.Tasks;
using MissionPlanner;

namespace test_unloading
{
    public class test_unloading : MissionPlanner.Plugin.Plugin
    {
        public override string Name { get; }
        public override string Version { get; }
        public override string Author { get; }

        private int loop_count = 0;

        public override bool Init()
        {
            return true;         
        }

        public override bool Loaded()
        {
            this.loopratehz = 1f;
            return true;
        }

        public override bool Loop()
        {
            loop_count++;
            Console.WriteLine($"Plugin Loop Count: {loop_count}");

            if (loop_count >= 5)
            {
                  this.loopratehz = 0;
                  Console.WriteLine("Unloading plugin after 5 loops.");
                  MissionPlanner.Plugin.PluginLoader.Plugins.Remove(this); // Triggers Exit()??
            }
            return true;
        }

        public override bool Exit()
        {
            Console.WriteLine("Plugin Unloaded.");
            return true;
        }
    }
}

Any advice or clarification would be greatly appreciated.
Thank you!

You cannot unload a plugin, at least there is no mechanism coded in MP to unload it.

Would be a nice debug feature to add someday…

I never added it because say you modify a UI element. Then. I unload the backing DLL. It will cause unhandled exceptions. It’s better to disable than unload when I’m not the one in control of the contents