App.xaml.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using DBMaintenanceTool.Utilities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Forms;
  12. using System.Windows.Threading;
  13. using Vinno.IUS.Common.Log;
  14. namespace DBMaintenanceTool
  15. {
  16. /// <summary>
  17. /// App.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class App : System.Windows.Application
  20. {
  21. [DllImport("user32.dll")]
  22. public static extern bool CloseWindow(IntPtr hWnd);
  23. protected override void OnStartup(StartupEventArgs e)
  24. {
  25. Exit += (sender, ee)=> {
  26. foreach (var port in new string[] { "19100", "19200" })
  27. {
  28. using (Process ps = new Process())
  29. {
  30. try
  31. {
  32. ps.StartInfo.FileName = $"{EnvironmentManager.MongoToolsPath}\\mongo";
  33. ps.StartInfo.UseShellExecute = false;
  34. ps.StartInfo.RedirectStandardOutput = false;
  35. ps.StartInfo.RedirectStandardInput = true;
  36. ps.StartInfo.Arguments = $" --host 127.0.0.1:{port}";
  37. ps.StartInfo.CreateNoWindow = true;
  38. ps.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  39. ps.StartInfo.Verb = "runas";
  40. ps.Start();
  41. ps.StandardInput.WriteLine($"use admin");
  42. ps.StandardInput.WriteLine(@"db.shutdownServer();");
  43. ps.StandardInput.WriteLine("exit");
  44. Logger.WriteLineInfo($"Stop DB port:{port} success");
  45. }
  46. catch (Exception ex)
  47. {
  48. Logger.WriteLineError($"Stop DB service error {ex}");
  49. throw;
  50. }
  51. }
  52. }
  53. };
  54. AppDomain.CurrentDomain.UnhandledException += OnDomainUnhandledException;
  55. Current.DispatcherUnhandledException += OnCurrentDispatcherUnhandledException;
  56. base.OnStartup(e);
  57. }
  58. private void OnCurrentDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  59. {
  60. Logger.WriteLineError($"DispatcherUnhandledException happened, detail is :{e.Exception}");
  61. e.Handled = true;
  62. }
  63. private void OnDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
  64. {
  65. Logger.WriteLineError($"DomainUnhandledException happened, detail is :{e.ExceptionObject}");
  66. //e.ExceptionObject.Handled = true;
  67. }
  68. }
  69. }