/// <summary> /// This will disable bot and close this process /// </summary> public static void Kill() { try { if (!IsRunning) { Syslog.DebugLog("Attempt to kill bot while it's already being killed", 2); return; } _Status = Status.ShuttingDown; Instance.Kill(); StorageWriter.IsRunning = false; Thread modules = new Thread(Terminate) { Name = "KERNEL: Core helper shutdown thread" }; modules.Start(); Syslog.WriteNow("Giving grace time for all modules to finish ok"); int kill = 0; while (kill < 20) { kill++; if (ExtensionHandler.ExtensionList.Count == 0) { Syslog.WriteNow("KERNEL: Modules are all down"); if (WriterThread.ThreadState == ThreadState.Running || WriterThread.ThreadState == ThreadState.WaitSleepJoin) { Syslog.WriteNow("KERNEL: Writer thread didn't shut down gracefully, waiting 2 seconds", true); Thread.Sleep(2000); if (WriterThread.ThreadState == ThreadState.Running || WriterThread.ThreadState == ThreadState.WaitSleepJoin) { Syslog.WriteNow("KERNEL: Writer thread didn't shut down gracefully, killing", true); WriterThread.Abort(); } else { Syslog.WriteNow("KERNEL: Writer thread is shut down", true); } } else { Syslog.WriteNow("KERNEL: Writer thread is down ok"); } break; } Thread.Sleep(1000); } if (ExtensionHandler.ExtensionList.Count == 0) { Syslog.WriteNow("KERNEL: Giving a grace time to other threads to finish"); Thread.Sleep(200); Syslog.WriteNow("KERNEL: Terminated (ok)"); Environment.Exit(0); } } catch (Exception fail) { HandleException(fail); } Syslog.WriteNow("There was problem shutting down " + ExtensionHandler.ExtensionList.Count + " modules, terminating process"); Syslog.WriteNow("KERNEL: Terminated (error)"); Process.GetCurrentProcess().Kill(); }