Bloom.Program.RestartBloom C# (CSharp) Method

RestartBloom() public static method

public static RestartBloom ( ) : void
return void
        public static void RestartBloom()
        {
            try
            {
                Process.Start(Application.ExecutablePath);

                //give some time for that process.start to finish staring the new instance, which will see
                //we have a mutex and wait for us to die.

                Thread.Sleep(2000);
                Environment.Exit(-1); //Force termination of the current process.
            }
            catch (Exception e)
            {
                ErrorReport.NotifyUserOfProblem(e, "Bloom encountered a problem while restarting.");
            }
        }

Usage Example

Example #1
0
        protected override void OnClosing(CancelEventArgs e)
        {
            //get everything saved (under the old collection name, if we are changing the name and restarting)
            _libraryClosingEvent.Raise(null);

            if (!string.IsNullOrEmpty(_nameToChangeCollectionUponClosing) &&
                _nameToChangeCollectionUponClosing != _collectionSettings.CollectionName &&
                UserWantsToOpeReopenProject)
            {
                // Without checking and resetting this flag, Linux endlessly spawns new instances. Apparently the Mono runtime
                // calls OnClosing again as a result of calling Program.RestartBloom() which calls Application.Exit().
                UserWantsToOpeReopenProject = false;
                //Actually restart Bloom with a parameter requesting this name change. It's way more likely to succeed
                //when this run isn't holding onto anything.
                try
                {
                    var existingDirectoryPath = Path.GetDirectoryName(_collectionSettings.SettingsFilePath);
                    var parentDirectory       = Path.GetDirectoryName(existingDirectoryPath);
                    var newDirectoryPath      = Path.Combine(parentDirectory, _nameToChangeCollectionUponClosing);

                    Program.RestartBloom(string.Format("--rename \"{0}\" \"{1}\" ", existingDirectoryPath, newDirectoryPath));
                }
                catch (Exception error)
                {
                    SIL.Reporting.ErrorReport.NotifyUserOfProblem(error,
                                                                  "Sorry, Bloom failed to even prepare for the rename of the project to '{0}'", _nameToChangeCollectionUponClosing);
                }
            }

            base.OnClosing(e);
        }