Пример #1
0
        private async void Shutdown_Click(object sender, RoutedEventArgs e)
        {
            if (ShutdownStarted)
            {
                return;
            }

            try
            {
                MessageOutput.Clear();

                ShutdownType = 1;
                Application.Current.Dispatcher.Invoke(() => this.Cursor = System.Windows.Input.Cursors.Wait);
                Application.Current.Dispatcher.Invoke(() => this.CancelShutdownButton.Cursor = System.Windows.Input.Cursors.Arrow);

                var app = new ServerApp(true)
                {
                    BackupWorldFile  = this.BackupWorldFile,
                    ShutdownInterval = this.ShutdownInterval,
                    ShutdownReason   = this.ShutdownReason,
                    OutputLogs       = false,
                    SendAlerts       = true,
                    ServerProcess    = RestartServer ? ServerProcessType.Restart : ServerProcessType.Shutdown,
                    ProgressCallback = (p, m, n) => { TaskUtils.RunOnUIThreadAsync(() => { this.AddMessage(m, n); }).DoNotWait(); },
                };

                var profile       = ProfileSnapshot.Create(Server.Profile);
                var restartServer = RestartServer;
                var updateServer  = UpdateServer;

                _shutdownCancellationSource = new CancellationTokenSource();

                var exitCode = await Task.Run(() => app.PerformProfileShutdown(profile, restartServer, updateServer, _shutdownCancellationSource.Token));

                if (exitCode != ServerApp.EXITCODE_NORMALEXIT && exitCode != ServerApp.EXITCODE_CANCELLED)
                {
                    throw new ApplicationException($"An error occured during the shutdown process - ExitCode: {exitCode}");
                }

                ShutdownType = 0;
                // if restarting or updating the server after the shutdown, delay the form closing
                if (restartServer || updateServer)
                {
                    await Task.Delay(5000);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _globalizer.GetResourceString("ServerSettings_ShutdownServer_FailedTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
                ShutdownType = 0;
            }
            finally
            {
                _shutdownCancellationSource = null;
                Application.Current.Dispatcher.Invoke(() => this.Cursor = null);
                Application.Current.Dispatcher.Invoke(() => this.CancelShutdownButton.Cursor = null);
            }
        }
        public async Task StartShutdownAsync()
        {
            if (ShutdownStarted)
            {
                return;
            }

            try
            {
                MessageOutput.Clear();

                ShutdownType = 1;
                Application.Current.Dispatcher.Invoke(() => this.Cursor = System.Windows.Input.Cursors.Wait);
                Application.Current.Dispatcher.Invoke(() => this.CancelShutdownButton.Cursor = System.Windows.Input.Cursors.Arrow);

                var app = new ServerApp(true)
                {
                    CheckForOnlinePlayers = this.CheckForOnlinePlayers,
                    SendMessages          = this.SendShutdownMessages,
                    BackupWorldFile       = this.BackupWorldFile,
                    ShutdownInterval      = this.ShutdownInterval,
                    ShutdownReason        = this.ShutdownReason,
                    OutputLogs            = false,
                    SendAlerts            = true,
                    ServerProcess         = RestartServer ? ServerProcessType.Restart : ServerProcessType.Shutdown,
                    ProgressCallback      = (p, m, n) => { TaskUtils.RunOnUIThreadAsync(() => { this.AddMessage(m, n); }).DoNotWait(); },
                };

                // if restarting the serverm, then check and update the public IP address
                if (RestartServer && Config.Default.ManagePublicIPAutomatically)
                {
                    await App.DiscoverMachinePublicIPAsync(false);
                }

                var profile       = ServerProfileSnapshot.Create(Server.Profile);
                var restartServer = RestartServer;
                var updateServer  = UpdateServer;

                _shutdownCancellationSource = new CancellationTokenSource();

                var exitCode = await Task.Run(() => app.PerformProfileShutdown(profile, restartServer, updateServer, false, _shutdownCancellationSource.Token));

                if (exitCode != ServerApp.EXITCODE_NORMALEXIT && exitCode != ServerApp.EXITCODE_CANCELLED)
                {
                    throw new ApplicationException($"An error occured during the shutdown process - ExitCode: {exitCode}");
                }

                if (restartServer)
                {
                    profile.Update(Server.Profile);
                    Server.Profile.SaveProfile();
                }

                ShutdownType = 0;
                // if restarting or updating the server after the shutdown, delay the form closing
                if (restartServer || updateServer)
                {
                    await Task.Delay(5000);
                }

                if (CloseShutdownWindowWhenFinished)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, _globalizer.GetResourceString("ServerSettings_ShutdownServer_FailedTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
                ShutdownType = 0;
            }
            finally
            {
                _shutdownCancellationSource = null;
                Application.Current.Dispatcher.Invoke(() => this.Cursor = null);
                Application.Current.Dispatcher.Invoke(() => this.CancelShutdownButton.Cursor = null);
            }
        }