/// <summary>
        /// Disconnect the event handlers from the text box to dispose of this instance
        /// </summary>
        /// <remarks>WPF text boxes don't implement <c>IDisposable</c> so we'll manage it ourselves</remarks>
        public void Disconnect()
        {
            if (!Microsoft.VisualStudio.Shell.ThreadHelper.CheckAccess())
            {
                // Fire and forget
                Task.Run(async() =>
                {
                    await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    this.Disconnect();
                }).Forget();

                return;
            }

            var window = Window.GetWindow(textBox);

            if (window != null && !window.GetType().FullName.StartsWith("Microsoft.VisualStudio.", StringComparison.OrdinalIgnoreCase))
            {
                window.Closing -= window_Closing;
            }

            textBox.TextChanged -= this.textBox_TextChanged;

            if (textBox.ContextMenu != null)
            {
                textBox.ContextMenu.Opened -= this.textBox_ContextMenuOpeningHandler;

                foreach (var m in textBox.ContextMenu.Items.OfType <FrameworkElement>().Where(m => m.Tag == this).ToArray())
                {
                    textBox.ContextMenu.Items.Remove(m);
                }
            }

            if (adorner != null)
            {
                adorner.Disconnect();
            }

            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
                timer = null;
            }

            wpfSpellCheckers.TryRemove(textBox, out WpfTextBoxSpellChecker value);

            System.Diagnostics.Debug.WriteLine("******* Disconnected from " + ElementName(textBox) + " *******");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Disconnect the event handlers from the text box to dispose of this instance
        /// </summary>
        /// <remarks>WPF text boxes don't implement <c>IDisposable</c> so we'll manage it ourselves</remarks>
        public async void Disconnect()
        {
            WpfTextBoxSpellChecker value;

            if (!textBox.CheckAccess())
            {
                await textBox.Dispatcher.InvokeAsync(() => this.Disconnect());

                return;
            }

            var window = Window.GetWindow(textBox);

            if (window != null && !window.GetType().FullName.StartsWith("Microsoft.VisualStudio.", StringComparison.OrdinalIgnoreCase))
            {
                window.Closing -= window_Closing;
            }

            textBox.TextChanged -= this.textBox_TextChanged;

            if (textBox.ContextMenu != null)
            {
                textBox.ContextMenu.Opened -= this.textBox_ContextMenuOpeningHandler;

                foreach (var m in textBox.ContextMenu.Items.OfType <FrameworkElement>().Where(m => m.Tag == this).ToArray())
                {
                    textBox.ContextMenu.Items.Remove(m);
                }
            }

            if (adorner != null)
            {
                adorner.Disconnect();
            }

            wpfSpellCheckers.TryRemove(textBox, out value);

            System.Diagnostics.Debug.WriteLine("******* Disconnected from " + ElementName(textBox) + " *******");
        }