Bootstrapper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. BaseApp.Instance = new VirgoUI.Client.ClientApp();
  40. DisplayRootViewFor<MainViewModel>();
  41. base.OnStartup(sender, e);
  42. }
  43. protected override void OnExit(object sender, EventArgs e)
  44. {
  45. base.OnExit(sender, e);
  46. Singleton<Aitex.Core.RT.Log.LogManager>.Instance.Terminate();
  47. MsgPool.TerminateAll();
  48. Application.Current.Shutdown();
  49. }
  50. protected override void OnUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  51. {
  52. if (e.Exception != null)
  53. {
  54. LOG.Write(e.Exception);
  55. if (e.Exception.InnerException != null)
  56. LOG.Write(e.Exception.InnerException);
  57. }
  58. e.Handled = true;
  59. }
  60. private SimpleContainer container;
  61. public Bootstrapper()
  62. {
  63. SciChartSurface.SetRuntimeLicenseKey("vwAIyu6FysOMMV/v7pag3WEENpK0hDMQx5zbMKYtlb3PMTsV9R+v2toHl/Z2YJ7t/MDVkKCBsZjBxOUs1djpkyLqgCFMlX5DMrKFdP82QRqZRCOht0hQjW5Omy5z9ZRbalxSx4mlgnL/YXWr2JQrD1dWoTeoDP7xm8JB3+KZ4M5pqxeUCib6VvRpfq3O7HVIyFfcDk0JVByjV+vtgGpOo5RP630lKr9VLS3CPk1aUeul4XQAnJX+IafLnsgSKiDWlZMYU9qJehqA1EdwOnEvvOwhcwckJ5/BoeQ0qDvDaYZ1Jfzkcv5sqOYKd749TJ8wsoTDubT/bLv+BwBiXura1mBZlOIE9zB5XwJVedWWzi6dGDG8LRRKh1XjuyD6V92G596xYsb5b8EJJ8AkgsC/R+fLeN/FIZBlq/Vepg+1dwkLlCCtp8nZWBjsRDQWNrG6Vyk5TF2RzI62WrwKfWfNWXtC5wjkJE5IJzUFlj/B/UhCzB8=");
  64. Initialize();
  65. }
  66. protected override void Configure()
  67. {
  68. container = new SimpleContainer();
  69. container.Singleton<IEventAggregator, EventAggregator>();
  70. container.Singleton<IWindowManager, WindowManager>();
  71. //container.Singleton<IPublisher, Publisher>();
  72. container.PerRequest<MainViewModel>();
  73. }
  74. void CheckAnotherInstanceRunning(string appName)
  75. {
  76. _mutex = new Mutex(true, appName, out bool createNew);
  77. if (!createNew)
  78. {
  79. if (!_mutex.WaitOne(1000))
  80. {
  81. string msg = $"{appName} is already running,can not start again";
  82. System.Windows.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.MessageBoxButton.OK, MessageBoxImage.Warning, System.Windows.MessageBoxResult.No,
  83. System.Windows.MessageBoxOptions.DefaultDesktopOnly);
  84. Environment.Exit(0);
  85. }
  86. }
  87. }
  88. protected override object GetInstance(Type service, string key)
  89. {
  90. return container.GetInstance(service, key);
  91. }
  92. protected override IEnumerable<object> GetAllInstances(Type service)
  93. {
  94. return container.GetAllInstances(service);
  95. }
  96. protected override void BuildUp(object instance)
  97. {
  98. container.BuildUp(instance);
  99. }
  100. }
  101. }