static void Main(string[] args) { List <Assembly_Threads> ATs = new List <Assembly_Threads>(); try { foreach (Assembly asm in EnumPluginFiles(@"E:\My Documents\Programming\Projects\Platform_Plugins\Plugins")) { Assembly_Threads at = new Assembly_Threads(asm); Thread curT = LoadDLL(asm, null); at.Add(curT); ATs.Add(at); curT.Start(); } } catch (Exception ex) { 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_BG_OnStop") == null) { throw new DllNotFoundException("The exit point of current DLL cannot be found."); } Thread th = new Thread(new ThreadStart( delegate { t.GetMethod("DOSSTONED_BG_OnStop").Invoke(null, null); } )); th.Start(); at.threads.Add(th); break; } /// wait for 1 second to wait other to terminate themselves. /// Thread.Sleep(1000); foreach (Thread t in at.threads) { if (t != null) { if (t.IsAlive) { t.Abort(); } } } }