App.xaml.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Diagnostics;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Windows;
  5. namespace FastGithub.UI
  6. {
  7. /// <summary>
  8. /// App.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class App : Application
  11. {
  12. private Mutex mutex;
  13. private Process fastGithub;
  14. protected override void OnStartup(StartupEventArgs e)
  15. {
  16. this.mutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance);
  17. if (firstInstance == false)
  18. {
  19. this.Shutdown();
  20. return;
  21. }
  22. this.fastGithub = StartFastGithub();
  23. base.OnStartup(e);
  24. }
  25. protected override void OnExit(ExitEventArgs e)
  26. {
  27. this.mutex.Dispose();
  28. if (this.fastGithub != null && this.fastGithub.HasExited == false)
  29. {
  30. this.fastGithub.Kill();
  31. }
  32. base.OnExit(e);
  33. }
  34. private static Process StartFastGithub()
  35. {
  36. const string fileName = "fastgithub.exe";
  37. if (File.Exists(fileName) == false)
  38. {
  39. return default;
  40. }
  41. var startInfo = new ProcessStartInfo
  42. {
  43. FileName = fileName,
  44. UseShellExecute = false,
  45. CreateNoWindow = true
  46. };
  47. return Process.Start(startInfo);
  48. }
  49. }
  50. }