using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; using Aitex.Core.RT.Log; using Aitex.Core.UI.View; using System.Windows.Media.Imaging; using Aitex.Core.UI.View.Frame; using System.Threading; using Aitex.Core.RT.ConfigCenter; using Aitex.Triton160.UI; namespace Triton160 { /// /// Interaction logic for App.xaml /// public partial class App : Application { static Mutex _mutex; static bool AnotherInstanceIsRunning() { try { string applicationName = "Triton160UI"; //createnew single app without throw an erro bool createNew; _mutex = new Mutex(true, applicationName, out createNew); return !createNew; } catch (Exception) { return false; } } public App() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException); } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { ProcessUnexpectedError(e.ExceptionObject as Exception); } private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { e.Handled = true; ProcessUnexpectedError(e.Exception); } private void ProcessUnexpectedError(Exception ex) { LOG.Write(ex, "UI exception"); 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); Environment.Exit(0); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); CheckAnotherInstanceRunning("Triton160.UI"); //if (AnotherInstanceIsRunning()) //{ // if (!_mutex.WaitOne(1000)) // { // System.Windows.Forms.MessageBox.Show("Triton160 Already Running。\r\n", "Triton160 start up", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); // Environment.Exit(0); // } //} Triton160UiSystem.Instance.Initialize(); } void CheckAnotherInstanceRunning(string appName) { _mutex = new Mutex(true, appName, out bool createNew); if (!createNew) { if (!_mutex.WaitOne(1000)) { string msg = $"{appName} is already running,can not start again"; System.Windows.Forms.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); Environment.Exit(0); } } } protected override void OnExit(ExitEventArgs e) { Triton160UiSystem.Instance.Terminate(); base.OnExit(e); } } }