public HResult Initialize()
        {
            this.Dispatcher = Dispatcher.FromThread(Thread.CurrentThread);

            if (this.Dispatcher == null)
            {
                return(HResult.FromException(new InvalidOperationException()));
            }

            HResult hr = this.PreThreadStartInitialize();

            if (hr.Failed)
            {
                return(hr);
            }

            this.processorThread = new Thread(this.ProcessRequests);
            this.processorThread.SetApartmentState(this.ApartmentState);
            this.processorThread.IsBackground = true;
            this.processorThread.Name         = "BackgroundRequestWorkerThread";
            this.processorThread.Start();

            if (!this.initHandshakeEvent.Wait(this.StartupTimeout))
            {
                return(HResult.FromException(new TimeoutException()));
            }

            if (this.initResult.Failed)
            {
                return(this.initResult);
            }

            return(this.PostThreadStartInitialize());
        }
        void OnOkButtonClicked(object sender, RoutedEventArgs e)
        {
            if (this.consoleInEditMode != null)
            {
                SetConsoleInEditMode(null, false);
                return;
            }

            var selectedConsole = this.listview.SelectedItem as ConsoleWrapper;

            if (selectedConsole == null)
            {
                return;
            }

            if (this.changesMade)
            {
                var dupeTable = new Dictionary <string, ConsoleWrapper>();

                // Check the console aliases for duplicates
                foreach (var c in this.Consoles)
                {
                    ConsoleWrapper dupe;

                    if (dupeTable.TryGetValue(c.Alias, out dupe))
                    {
                        this.notificationService.ShowError(string.Format(StringResources.ConsoleAliasAlreadyExistsFmt, c.Alias), HResult.FromErrorText(StringResources.ConsoleAliasesMustBeUnique));
                        this.listview.SelectedItem = c;
                        return;
                    }
                    dupeTable[c.Alias] = c;
                }

                // No duplicates... let's try to make the console manager look like what we've got.
                try
                {
                    // First, nuke the pure removals
                    foreach (var c in this.removals)
                    {
                        // NOTE:  Remove by the real console alias.  The user might have changed the local one...
                        this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                    }

                    // Now remove anything that has changed.  The replacements list ends up being all consoles that
                    // need to be written, either because they're new, or changed (removed + added).
                    var replacements = new List <ConsoleWrapper>();

                    foreach (var c in this.Consoles)
                    {
                        if (c.RealConsole != null && (c.RealConsole.Alias != c.Alias || c.RealConsole.Address != c.Address))
                        {
                            this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                            replacements.Add(c);
                        }
                        else if (c.RealConsole == null)
                        {
                            replacements.Add(c);
                        }
                    }

                    // Re-add everything in replacements list
                    foreach (var c in replacements)
                    {
                        this.consoleManager.AddConsole(c.Alias, c.Address);
                    }

                    // Set the default kit
                    var defaultConsole = this.Consoles.FirstOrDefault(c => c.IsDefault);

                    if (defaultConsole != null)
                    {
                        // NOTE:  If the user deletes the default console and doesn't set a new one, the console
                        // manager automatically clears the (now bogus) default console specification.
                        this.consoleManager.SetDefaultConsole(defaultConsole.Alias);
                    }
                }
                catch (Exception ex)
                {
                    this.notificationService.ShowError(HResult.FromException(ex));

                    // This is somewhat bad... we possibly made *some* of our changes, so chances are that
                    // trying again will end up failing again, even if the user fixed the original problem.
                    // Best hope is to reset the dialog to whatever state the actual list is in.
                    InitConsolesList();
                    return;
                }
            }

            if (selectedConsole.IsDefault)
            {
                this.SpecificKit = null;
            }
            else
            {
                this.SpecificKit = selectedConsole.Address;
            }

            this.SelectedConsole = new ConsoleIdentifier(selectedConsole.Alias, selectedConsole.Address, selectedConsole.IsDefault);

            // Re-update the outstanding console identifiers
            ConsoleIdentifier.UpdateOutstandingIdentifiers(this.consoleManager.GetDefaultConsole(), this.consoleManager.GetConsoles());
            DialogResult = true;
        }
Esempio n. 3
0
        void LoadWindowState()
        {
            bool usingDefault = false;

            try
            {
                if (!File.Exists(this.windowStateFileName))
                {
                    usingDefault = true;
                    CreateDefaultWindowState();
                }

                TryLoadWindowState();
            }
            catch (Exception ex)
            {
                bool versionMismatch = ex is VersionMismatchException;

                // Don't let missing/corrupt window state data tip us over.  Try the default if we haven't already.
                if (!usingDefault)
                {
                    ex = null;

                    try
                    {
                        CreateDefaultWindowState();
                        TryLoadWindowState();
                    }
                    catch (Exception ex2)
                    {
                        // This will leave our window blank.  Bad, but the file tab will still work.  When we have a tools/options/reset window state option,
                        // that will be the solution (or at least the way we can report the problem in a natural way).  This would be a weird
                        // situation anyway...
                        ex = ex2;
                    }
                }

                if (ex != null || versionMismatch)
                {
                    this.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        var notificationService = this.RootServiceProvider.GetService(typeof(IUserNotificationService)) as IUserNotificationService;

                        if (ex != null)
                        {
                            var loggingService = this.RootServiceProvider.GetService(typeof(ILoggingService)) as ILoggingService;

                            if (loggingService != null)
                            {
                                loggingService.LogException(ex);
                            }

                            if (notificationService != null)
                            {
                                notificationService.ShowError(StringResources.ErrorLoadingWindowState, HResult.FromException(ex));
                            }
                        }
                        else
                        {
                            if (notificationService != null)
                            {
                                notificationService.ShowMessageBox(StringResources.WindowStateVersionMismatch);
                            }
                        }
                    }), DispatcherPriority.Background);
                }
            }
        }