Bootstrapper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Windows;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.WCF;
  8. using Caliburn.Micro;
  9. using Caliburn.Micro.Core;
  10. using OpenSEMI.ClientBase;
  11. using OpenSEMI.Core.Msg;
  12. using SciChart.Charting.Visuals;
  13. using Virgo_D.UI.Config;
  14. namespace VirgoUI.Client
  15. {
  16. public class Bootstrapper : BootstrapperBase
  17. {
  18. private static Mutex _mutex;
  19. protected override void OnStartup(object sender, StartupEventArgs e)
  20. {
  21. try
  22. {
  23. CheckAnotherInstanceRunning("Virgo.UI");
  24. if (!EventClient.Instance.ConnectRT())
  25. {
  26. MessageBox.Show("Can not connect with RT, launch RT first", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  27. Environment.Exit(0);
  28. return;
  29. }
  30. }
  31. catch (Exception)
  32. {
  33. MessageBox.Show("Can not connect with RT, launch RT first", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  34. Environment.Exit(0);
  35. return;
  36. }
  37. SystemConfigManager.Instance.Initialize();
  38. Singleton<Aitex.Core.RT.Log.LogManager>.Instance.Initialize();
  39. FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
  40. BaseApp.Instance = new VirgoUI.Client.ClientApp();
  41. DisplayRootViewFor<MainViewModel>();
  42. base.OnStartup(sender, e);
  43. }
  44. protected override void OnExit(object sender, EventArgs e)
  45. {
  46. base.OnExit(sender, e);
  47. Singleton<Aitex.Core.RT.Log.LogManager>.Instance.Terminate();
  48. MsgPool.TerminateAll();
  49. Application.Current.Shutdown();
  50. }
  51. protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  52. {
  53. if (e.Exception != null)
  54. {
  55. LOG.Write(e.Exception);
  56. if (e.Exception.InnerException != null)
  57. LOG.Write(e.Exception.InnerException);
  58. }
  59. e.Handled = true;
  60. }
  61. private SimpleContainer container;
  62. public Bootstrapper()
  63. {
  64. SciChartSurface.SetRuntimeLicenseKey("vwAIyu6FysOMMV/v7pag3WEENpK0hDMQx5zbMKYtlb3PMTsV9R+v2toHl/Z2YJ7t/MDVkKCBsZjBxOUs1djpkyLqgCFMlX5DMrKFdP82QRqZRCOht0hQjW5Omy5z9ZRbalxSx4mlgnL/YXWr2JQrD1dWoTeoDP7xm8JB3+KZ4M5pqxeUCib6VvRpfq3O7HVIyFfcDk0JVByjV+vtgGpOo5RP630lKr9VLS3CPk1aUeul4XQAnJX+IafLnsgSKiDWlZMYU9qJehqA1EdwOnEvvOwhcwckJ5/BoeQ0qDvDaYZ1Jfzkcv5sqOYKd749TJ8wsoTDubT/bLv+BwBiXura1mBZlOIE9zB5XwJVedWWzi6dGDG8LRRKh1XjuyD6V92G596xYsb5b8EJJ8AkgsC/R+fLeN/FIZBlq/Vepg+1dwkLlCCtp8nZWBjsRDQWNrG6Vyk5TF2RzI62WrwKfWfNWXtC5wjkJE5IJzUFlj/B/UhCzB8=");
  65. Initialize();
  66. }
  67. protected override void Configure()
  68. {
  69. container = new SimpleContainer();
  70. container.Singleton<IEventAggregator, EventAggregator>();
  71. container.Singleton<IWindowManager, WindowManager>();
  72. //container.Singleton<IPublisher, Publisher>();
  73. container.PerRequest<MainViewModel>();
  74. }
  75. void CheckAnotherInstanceRunning(string appName)
  76. {
  77. _mutex = new Mutex(true, appName, out bool createNew);
  78. if (!createNew)
  79. {
  80. if (!_mutex.WaitOne(1000))
  81. {
  82. string msg = $"{appName} is already running,can not start again";
  83. System.Windows.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.MessageBoxButton.OK, MessageBoxImage.Warning, System.Windows.MessageBoxResult.No,
  84. System.Windows.MessageBoxOptions.DefaultDesktopOnly);
  85. Environment.Exit(0);
  86. }
  87. }
  88. }
  89. protected override object GetInstance(Type service, string key)
  90. {
  91. return container.GetInstance(service, key);
  92. }
  93. protected override IEnumerable<object> GetAllInstances(Type service)
  94. {
  95. return container.GetAllInstances(service);
  96. }
  97. protected override void BuildUp(object instance)
  98. {
  99. container.BuildUp(instance);
  100. }
  101. }
  102. }