Esempio n. 1
0
        private async void Application_Startup(object sender, StartupEventArgs e)
        {
            // retrieve file argument if given
            if (e.Args.Length > 0)
            {
                if (!e.Args[0].Equals(Constants.Argument_Ask_For_Admin, StringComparison.OrdinalIgnoreCase))
                {
                    FilenameArgument = e.Args[0];
                }
            }

            using var mutexAnnoDesigner = new Mutex(true, MutexHelper.MUTEX_ANNO_DESIGNER, out var createdNewMutex);
            //Are there other processes still running?
            if (!createdNewMutex)
            {
                try
                {
                    var       currentTry = 0;
                    const int maxTrys    = 10;
                    while (!createdNewMutex && currentTry < maxTrys)
                    {
                        logger.Trace($"Waiting for other processes to finish. Try {currentTry} of {maxTrys}");

                        createdNewMutex = mutexAnnoDesigner.WaitOne(TimeSpan.FromSeconds(1), true);
                        currentTry++;
                    }

                    if (!createdNewMutex)
                    {
                        _messageBoxService.ShowMessage(Localization.Localization.Instance.GetLocalization("AnotherInstanceIsAlreadyRunning"));
                        Environment.Exit(-1);
                    }
                }
                catch (AbandonedMutexException)
                {
                    //mutex was killed
                    createdNewMutex = true;
                }
            }

            try
            {
                //check if file is not corrupt
                _appSettings.Reload();

                if (_appSettings.SettingsUpgradeNeeded)
                {
                    _appSettings.Upgrade();
                    _appSettings.SettingsUpgradeNeeded = false;
                    _appSettings.Save();
                }
            }
            catch (ConfigurationErrorsException ex)
            {
                logger.Error(ex, "Error upgrading settings.");

                _messageBoxService.ShowError(_localizationHelper.GetLocalization("ErrorUpgradingSettings"));

                var fileName = "";
                if (!string.IsNullOrEmpty(ex.Filename))
                {
                    fileName = ex.Filename;
                }
                else
                {
                    if (ex.InnerException is ConfigurationErrorsException innerException && !string.IsNullOrEmpty(innerException.Filename))
                    {
                        fileName = innerException.Filename;
                    }
                }

                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                _appSettings.Reload();
            }

            //var updateWindow = new UpdateWindow();
            await _updateHelper.ReplaceUpdatedPresetsFilesAsync();

            var recentFilesSerializer = new RecentFilesAppSettingsSerializer(_appSettings);

            IRecentFilesHelper recentFilesHelper = new RecentFilesHelper(recentFilesSerializer, _fileSystem);
            var mainVM = new MainViewModel(_commons, _appSettings, recentFilesHelper, _messageBoxService, _updateHelper, _localizationHelper, _fileSystem);

            //TODO MainWindow.ctor calls AnnoCanvas.ctor loads presets -> change logic when to load data
            MainWindow             = new MainWindow(_appSettings);
            MainWindow.DataContext = mainVM;

            //If language is not recognized, bring up the language selection screen
            if (!_commons.LanguageCodeMap.ContainsKey(_appSettings.SelectedLanguage))
            {
                var w = new Welcome();
                w.DataContext = mainVM.WelcomeViewModel;
                w.ShowDialog();
            }
            else
            {
                _commons.CurrentLanguage = _appSettings.SelectedLanguage;
            }

            MainWindow.ShowDialog();
        }
Esempio n. 2
0
        private async void Application_Startup(object sender, StartupEventArgs e)
        {
            // retrieve file argument if given
            if (e.Args.Length > 0)
            {
                if (!e.Args[0].Equals(Constants.Argument_Ask_For_Admin, StringComparison.OrdinalIgnoreCase))
                {
                    FilenameArgument = e.Args[0];
                }
            }

            using (var mutexAnnoDesigner = new Mutex(true, MutexHelper.MUTEX_ANNO_DESIGNER, out bool createdNewMutex))
            {
                //Are there other processes still running?
                if (!createdNewMutex)
                {
                    try
                    {
                        var       currentTry = 0;
                        const int maxTrys    = 10;
                        while (!createdNewMutex && currentTry < maxTrys)
                        {
                            logger.Trace($"Waiting for other processes to finish. Try {currentTry} of {maxTrys}");

                            createdNewMutex = mutexAnnoDesigner.WaitOne(TimeSpan.FromSeconds(1), true);
                            currentTry++;
                        }

                        if (!createdNewMutex)
                        {
                            MessageBox.Show("Another instance of the app is already running.");
                            Environment.Exit(-1);
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        //mutex was killed
                        createdNewMutex = true;
                    }
                }

                try
                {
                    //check if file is not corrupt
                    _appSettings.Reload();

                    if (_appSettings.SettingsUpgradeNeeded)
                    {
                        _appSettings.Upgrade();
                        _appSettings.SettingsUpgradeNeeded = false;
                        _appSettings.Save();
                    }
                }
                catch (ConfigurationErrorsException ex)
                {
                    logger.Error(ex, "Error upgrading settings.");

                    MessageBox.Show("The settings file has become corrupted. We must reset your settings.",
                                    "Error",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);

                    var fileName = "";
                    if (!string.IsNullOrEmpty(ex.Filename))
                    {
                        fileName = ex.Filename;
                    }
                    else
                    {
                        var innerException = ex.InnerException as ConfigurationErrorsException;
                        if (innerException != null && !string.IsNullOrEmpty(innerException.Filename))
                        {
                            fileName = innerException.Filename;
                        }
                    }

                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    _appSettings.Reload();
                }

                //var updateWindow = new UpdateWindow();
                await _commons.UpdateHelper.ReplaceUpdatedPresetsFilesAsync();

                var mainVM = new MainViewModel(_commons, _appSettings);

                //TODO MainWindow.ctor calls AnnoCanvas.ctor loads presets -> change logic when to load data
                MainWindow             = new MainWindow();
                MainWindow.DataContext = mainVM;
                //MainWindow.Loaded += (s, args) => { updateWindow.Close(); };

                //updateWindow.Show();

                //If language is not recognized, bring up the language selection screen
                if (!Localization.Localization.LanguageCodeMap.ContainsKey(_appSettings.SelectedLanguage))
                {
                    var w = new Welcome();
                    w.DataContext = mainVM.WelcomeViewModel;
                    w.ShowDialog();
                }
                else
                {
                    _commons.SelectedLanguage = _appSettings.SelectedLanguage;
                }

                MainWindow.ShowDialog();

                //base.OnStartup(e);//needed?
            }
        }