App.xaml.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Data;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Windows;
  8. using Vinno.FIS.Sonopost.Common;
  9. namespace Vinno.FIS.Sonopost.Keeper
  10. {
  11. /// <summary>
  12. /// App.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class App : Application
  15. {
  16. private string _sonopostPath;
  17. private ProcessKeepHelper _keepHelper;
  18. protected override void OnStartup(StartupEventArgs args)
  19. {
  20. try
  21. {
  22. base.OnStartup(args);
  23. CheckedOpenRepeatedly();
  24. Thread.Sleep(2000);
  25. try
  26. {
  27. ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.Upgrade");
  28. ProcessHelper.FinishProcessByName("Sonopost");
  29. ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.TRTCClient");
  30. }
  31. catch (Exception e)
  32. {
  33. WriteLog(e.ToString());
  34. }
  35. _sonopostPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sonopost.exe");
  36. var processId = ProcessHelper.StartProcess(_sonopostPath);
  37. _keepHelper = new ProcessKeepHelper(processId);
  38. _keepHelper.StartKeepProcess(() =>
  39. {
  40. return ProcessHelper.StartProcess(_sonopostPath);
  41. });
  42. }
  43. catch (Exception e)
  44. {
  45. WriteLog(e.ToString());
  46. }
  47. }
  48. protected override void OnExit(ExitEventArgs e)
  49. {
  50. _keepHelper?.StopKeepProcess();
  51. base.OnExit(e);
  52. }
  53. private void WriteLog(string msg)
  54. {
  55. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SonopostKeeperLog.log");
  56. File.AppendAllText(path, msg);
  57. }
  58. /// <summary>
  59. /// 判断是否重复打开
  60. /// </summary>
  61. private void CheckedOpenRepeatedly()
  62. {
  63. var thisProcess = Process.GetCurrentProcess();
  64. if (Process.GetProcessesByName(thisProcess.ProcessName).Where(x => x.ProcessName == thisProcess.ProcessName).Count() > 1)
  65. {
  66. thisProcess.Kill();
  67. }
  68. }
  69. }
  70. }