Exemplo n.º 1
0
        /// <summary>
        /// アプリケーション開始時
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void App_Startup(object sender, StartupEventArgs e)
        {
            Properties.Settings settings = HouseholdAccountBook.Properties.Settings.Default;

#if DEBUG
            Debug.Listeners.Add(new ConsoleTraceListener());
#endif

#if !DEBUG
            // 多重起動を抑止する
            App.mutex = new Mutex(false, this.GetType().Assembly.GetName().Name);
            if (!mutex.WaitOne(TimeSpan.Zero, false))
            {
                Process   curProcess  = Process.GetCurrentProcess();
                Process[] processList = Process.GetProcessesByName(curProcess.ProcessName);

                if (processList.Length >= 2)
                {
                    foreach (Process process in processList)
                    {
                        if (process.Id != curProcess.Id)
                        {
                            // 外部プロセスのアクティブ化したい(Win32を使わざるを得ない)
                        }
                    }
                }

                MessageBox.Show("同時に複数起動することはできません。", MessageTitle.Exclamation);
                this.Shutdown();
                return;
            }
#endif

            this.DispatcherUnhandledException += this.App_DispatcherUnhandledException;
            this.Exit += this.App_Exit;

            // 前バージョンからのUpgradeを実行していないときはUpgradeを実施する
            Version assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            if (!Version.TryParse(settings.App_Version, out Version preVersion) || preVersion < assemblyVersion)
            {
                // Upgradeを実行する
                settings.Upgrade();
            }

            // 初回起動時
            if (settings.App_InitFlag)
            {
#if !DEBUG
                // リリースビルドの初回起動時デバッグモードはOFF
                settings.App_IsDebug = false;
#endif
                // DB設定ダイアログ終了時に閉じないように設定する(明示的なシャットダウンが必要)
                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

                // データベース接続を設定する
                DbSettingWindow dsw    = new DbSettingWindow("DB接続設定を入力してください。");
                bool?           result = dsw.ShowDialog();

                if (result != true)
                {
                    this.connectInfo = null;
                    this.Shutdown();
                    return;
                }

                settings.App_InitFlag = false;
                settings.Save();
            }

            DaoBuilder builder = null;
            while (true)
            {
                // 接続設定を読み込む
                this.connectInfo = new DaoNpgsql.ConnectInfo()
                {
                    Host     = settings.App_Postgres_Host,
                    Port     = settings.App_Postgres_Port,
                    UserName = settings.App_Postgres_UserName,
                    Password = settings.App_Postgres_Password,
#if DEBUG
                    DatabaseName = settings.App_Postgres_DatabaseName_Debug,
#else
                    DatabaseName = settings.App_Postgres_DatabaseName,
#endif
                    Role = settings.App_Postgres_Role
                };
                builder = new DaoBuilder(this.connectInfo);

                // 接続を試行する
                bool isOpen = false;
                try {
                    using (DaoBase dao = builder.Build()) {
                        isOpen = dao.IsOpen;
                    }
                }
                catch (TimeoutException) { }

                if (isOpen)
                {
                    break;
                }
                else
                {
                    // データベース接続を設定する
                    DbSettingWindow dsw    = new DbSettingWindow("接続に失敗しました。接続設定を見直してください。");
                    bool?           result = dsw.ShowDialog();

                    if (result != true)
                    {
                        this.connectInfo = null;
                        this.Shutdown();
                        return;
                    }
                }
            }

            // 休日リストを取得する
            await DateTimeExtensions.DownloadHolidayListAsync();

            // 設定をリソースに登録する
            this.RegisterSettingsToResource();

            // DBに接続できる場合だけメインウィンドウを開く
            MainWindow mw = new MainWindow(builder);
            this.MainWindow   = mw;
            this.ShutdownMode = ShutdownMode.OnMainWindowClose;
            mw.Show();
        }