/// <summary> /// Unregister a dll /// </summary> /// <param name="at">The class of Assembly_Threads</param> public static void UnloadDLL(Assembly_Threads at) { Console.WriteLine("Test."); Type[] ts = at.asm.GetExportedTypes(); bool needToCallStop = false; foreach (Thread t in at.threads) { if (t != null) { if (t.IsAlive) { needToCallStop = true; break; } } } if (needToCallStop) { 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) { System.IO.File.AppendAllText(@"E:\TEMP\DOSSTONED_BG.txt", "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(); } } } }
protected override void OnStart(string[] args) { ATs = new List <Assembly_Threads>(); try { foreach (Assembly asm in DLLReg.EnumPluginFiles(@"C:\Users\Public\Documents\DOSSTONED_BG\Plugins")) { Assembly_Threads at = new Assembly_Threads(asm); Thread curT = DLLReg.LoadDLL(asm, null); at.Add(curT); ATs.Add(at); curT.Start(); } } catch (Exception ex) { System.IO.File.AppendAllText(@"E:\TEMP\DOSSTONED_BG.txt", "\r\n" + ex.Message + "\r\n"); System.IO.File.AppendAllText(@"E:\TEMP\DOSSTONED_BG.txt", ex.StackTrace + "\r\n"); Stop(); } }