Skip to content

Commit 80a6a79

Browse files
committed
Tear down VBE menus so Excel can exit.
1 parent 0f54623 commit 80a6a79

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

RetailCoder.VBE/App.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed class App : IDisposable
2626
private const string FILE_TARGET_NAME = "file";
2727
private readonly VBE _vbe;
2828
private readonly IMessageBox _messageBox;
29-
private readonly IRubberduckParser _parser;
29+
private IRubberduckParser _parser;
3030
private AutoSave.AutoSave _autoSave;
3131
private IGeneralConfigService _configService;
3232
private readonly IAppMenu _appMenus;
@@ -466,6 +466,16 @@ public void Dispose()
466466
_branchesVM.LoadingComponentsCompleted -= EnableSinkEventHandlersAndUpdateCache;
467467
}
468468

469+
_handleSinkEvents = false;
470+
471+
if (_parser != null && _parser.State != null)
472+
{
473+
_parser.State.StateChanged -= Parser_StateChanged;
474+
_parser.State.StatusMessageUpdate -= State_StatusMessageUpdate;
475+
_parser.Dispose();
476+
// I won't set this to null because other components may try to release things
477+
}
478+
469479
if (_hooks != null)
470480
{
471481
_hooks.MessageReceived -= _hooks_MessageReceived;
@@ -480,13 +490,6 @@ public void Dispose()
480490
_configService = null;
481491
}
482492

483-
if (_parser != null && _parser.State != null)
484-
{
485-
_parser.State.StateChanged -= Parser_StateChanged;
486-
_parser.State.StatusMessageUpdate -= State_StatusMessageUpdate;
487-
// I won't set this to null because other components may try to release things
488-
}
489-
490493
if (_stateBar != null)
491494
{
492495
_stateBar.Refresh -= _stateBar_Refresh;
@@ -529,6 +532,8 @@ public void Dispose()
529532
item.Value.Item1.Unadvise(item.Value.Item2);
530533
}
531534

535+
UiDispatcher.Shutdown();
536+
532537
_disposed = true;
533538
}
534539
}

RetailCoder.VBE/AppMenu.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using Rubberduck.Parsing.VBA;
45
using Rubberduck.UI.Command.MenuItems;
56
using Rubberduck.UI.Command.MenuItems.ParentMenus;
@@ -43,6 +44,7 @@ public void Dispose()
4344
{
4445
foreach (var menu in _menus)
4546
{
47+
menu.RemoveChildren();
4648
if (menu.Item != null)
4749
{
4850
menu.Item.Delete();

RetailCoder.VBE/UI/Command/MenuItems/ParentMenus/IParentMenuItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Microsoft.Office.Core;
23

34
namespace Rubberduck.UI.Command.MenuItems.ParentMenus
@@ -6,5 +7,6 @@ public interface IParentMenuItem : IMenuItem, IAppMenu
67
{
78
CommandBarControls Parent { get; set; }
89
CommandBarPopup Item { get; }
10+
void RemoveChildren();
911
}
1012
}

RetailCoder.VBE/UI/Command/MenuItems/ParentMenus/ParentMenuItemBase.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Drawing;
45
using System.Linq;
56
using System.Runtime.InteropServices;
@@ -34,7 +35,7 @@ protected ParentMenuItemBase(string key, IEnumerable<IMenuItem> items, int? befo
3435

3536
public virtual bool BeginGroup { get { return false; } }
3637
public virtual int DisplayOrder { get { return default(int); } }
37-
38+
3839
public void Localize()
3940
{
4041
if (Item == null)
@@ -81,6 +82,23 @@ public void Initialize()
8182
_logger.Debug("'{0}' ({1}) parent menu initialized, hash code {2}.", _key, GetHashCode(), Item.GetHashCode());
8283
}
8384

85+
public void RemoveChildren()
86+
{
87+
foreach (var child in _items.Keys.Select(item => item as IParentMenuItem).Where(child => child != null))
88+
{
89+
Debug.WriteLine("Deleting menu item {0}.", child.Caption);
90+
child.RemoveChildren();
91+
Debug.Assert(_items[child] is CommandBarPopup);
92+
(_items[child] as CommandBarPopup).Delete();
93+
}
94+
foreach (var child in _items.Values.Select(item => item as CommandBarButton).Where(child => child != null))
95+
{
96+
child.Click -= child_Click;
97+
Debug.WriteLine("Deleting child menu item {0}.", child.Caption);
98+
child.Delete();
99+
}
100+
}
101+
84102
public void EvaluateCanExecute(RubberduckParserState state)
85103
{
86104
foreach (var kvp in _items)

RetailCoder.VBE/UI/Command/MenuItems/UiDispatcher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@ private static void CheckInitialization()
5858
{
5959
if (UiContext == null) throw new InvalidOperationException("UiDispatcher is not initialized. Invoke Initialize() from UI thread first.");
6060
}
61+
62+
public static void Shutdown()
63+
{
64+
Invoke(() => Dispatcher.CurrentDispatcher.InvokeShutdown());
65+
}
6166
}
6267
}

Rubberduck.Parsing/IRubberduckParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using System;
12
using Rubberduck.Parsing.VBA;
23

34
namespace Rubberduck.Parsing
45
{
5-
public interface IRubberduckParser
6+
public interface IRubberduckParser : IDisposable
67
{
78
RubberduckParserState State { get; }
89
}

Rubberduck.Parsing/VBA/RubberduckParser.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Rubberduck.Parsing.VBA
2121
{
22-
public class RubberduckParser : IRubberduckParser, IDisposable
22+
public class RubberduckParser : IRubberduckParser
2323
{
2424
public RubberduckParserState State { get { return _state; } }
2525

@@ -603,16 +603,17 @@ private void ResolveReferences(DeclarationFinder finder, VBComponent component,
603603
public void Dispose()
604604
{
605605
State.ParseRequest -= ReparseRequested;
606-
if (_resolverTokenSource != null)
607-
{
608-
_resolverTokenSource.Dispose();
609-
}
610606

611607
if (_central != null)
612608
{
613-
_central.Cancel();
609+
//_central.Cancel();
614610
_central.Dispose();
615611
}
612+
613+
if (_resolverTokenSource != null)
614+
{
615+
_resolverTokenSource.Dispose();
616+
}
616617
}
617618
}
618619
}

0 commit comments

Comments
 (0)