void LoadPlugins(string path) { int i = 0; foreach (Assembly asm in EnumPluginFiles(path))//@"E:\My Documents\Programming\Projects\Platform_Plugins\Plugins")) { try { Assembly_Threads at = new Assembly_Threads(asm); //GroupBox gb = new GroupBox(); Panel gb = new Panel(); string asmName = at.asm.FullName.Split(',')[0]; Thread curT = LoadDLL(asm, new object[] { gb }); at.Add(curT); ATs.Add(at); curT.Start(); /// Add notification to the GUI //gb.Text = asmName; gb.Width = 600; gb.Height = 400; gb.Margin = new System.Windows.Forms.Padding(3); gb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom; tabControl1.TabPages.Add(asmName);//panelPlugins.Controls.Add(gb); tabControl1.TabPages[i++].Controls.Add(gb); listBoxLoadedPlugins.Items.Add("Plugin: " + asmName + " loaded."); } catch (Exception ex) { MessageBox.Show(ex.Message, asm.FullName.Split(',')[0]); //Console.WriteLine(ex.Message); } finally { //Console.WriteLine("Press Enter to stop."); //Console.Read(); //foreach (Assembly_Threads at in ATs) //{ // UnloadDLL(at); //} //Console.WriteLine("END. Press Enter to exit."); //Console.ReadLine(); } } }
static void UnloadDLL(Assembly_Threads at) { Type[] ts = at.asm.GetExportedTypes(); foreach (Type t in ts) { /// Deprecated, because this will call ToString method also, which leads to the exception. /// //foreach (MethodInfo minfo in t.GetMethods()) //{ // minfo.Invoke(null, param); //} if (t.GetMethod("DOSSTONED_FE_UNLOAD") == null) { throw new BadImageFormatException("The exit point of current DLL cannot be found."); } Thread th = new Thread(new ThreadStart( delegate { t.GetMethod("DOSSTONED_FE_UNLOAD").Invoke(null, null); } )); th.Start(); at.threads.Add(th); break; } /// wait for 0.1 second to wait other to terminate themselves. /// Thread.Sleep(100); foreach (Thread t in at.threads) { if (t != null) { if (t.IsAlive) { t.Abort(); } } } }