App.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Windows;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.UI.View;
  9. using System.Windows.Media.Imaging;
  10. using Aitex.Core.UI.View.Frame;
  11. using System.Threading;
  12. using Aitex.Core.RT.ConfigCenter;
  13. using Aitex.Triton160.UI;
  14. namespace Triton160
  15. {
  16. /// <summary>
  17. /// Interaction logic for App.xaml
  18. /// </summary>
  19. public partial class App : Application
  20. {
  21. static Mutex _mutex;
  22. static bool AnotherInstanceIsRunning()
  23. {
  24. try
  25. {
  26. string applicationName = "Triton160UI";
  27. //createnew single app without throw an erro
  28. bool createNew;
  29. _mutex = new Mutex(true, applicationName, out createNew);
  30. return !createNew;
  31. }
  32. catch (Exception)
  33. {
  34. return false;
  35. }
  36. }
  37. public App()
  38. {
  39. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  40. Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
  41. }
  42. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  43. {
  44. ProcessUnexpectedError(e.ExceptionObject as Exception);
  45. }
  46. private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  47. {
  48. e.Handled = true;
  49. ProcessUnexpectedError(e.Exception);
  50. }
  51. private void ProcessUnexpectedError(Exception ex)
  52. {
  53. LOG.Write(ex, "UI exception");
  54. MessageBox.Show(MainWindow, "Exception found during UI system running, please find detail information from the log file. \r\n\r\n please restart the ui system to restore.", "ui internal error", MessageBoxButton.OK, MessageBoxImage.Error);
  55. Environment.Exit(0);
  56. }
  57. protected override void OnStartup(StartupEventArgs e)
  58. {
  59. base.OnStartup(e);
  60. CheckAnotherInstanceRunning("Triton160.UI");
  61. //if (AnotherInstanceIsRunning())
  62. //{
  63. // if (!_mutex.WaitOne(1000))
  64. // {
  65. // System.Windows.Forms.MessageBox.Show("Triton160 Already Running。\r\n", "Triton160 start up", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  66. // Environment.Exit(0);
  67. // }
  68. //}
  69. Triton160UiSystem.Instance.Initialize();
  70. }
  71. void CheckAnotherInstanceRunning(string appName)
  72. {
  73. _mutex = new Mutex(true, appName, out bool createNew);
  74. if (!createNew)
  75. {
  76. if (!_mutex.WaitOne(1000))
  77. {
  78. string msg = $"{appName} is already running,can not start again";
  79. System.Windows.Forms.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  80. Environment.Exit(0);
  81. }
  82. }
  83. }
  84. protected override void OnExit(ExitEventArgs e)
  85. {
  86. Triton160UiSystem.Instance.Terminate();
  87. base.OnExit(e);
  88. }
  89. }
  90. }