Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit bf3e472

Browse files
committed
Implement Dispose pattern
1 parent 3de9fc2 commit bf3e472

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/GitHub.UI/Helpers/SharedDictionaryManager.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ public CachingFactory()
7272
disposables = new HashSet<IDisposable>();
7373
}
7474

75-
public void Dispose()
76-
{
77-
foreach (var disposable in disposables)
78-
{
79-
disposable.Dispose();
80-
}
81-
82-
disposables.Clear();
83-
sharedDictionaries.Clear();
84-
}
85-
8675
public ResourceDictionary GetOrCreateResourceDictionary(ResourceDictionary owner, Uri uri)
8776
{
8877
TryAddDisposable(owner);
@@ -106,6 +95,29 @@ public void TryAddDisposable(object owner)
10695
disposables.Add(disposable);
10796
}
10897
}
98+
99+
bool disposed;
100+
void Dispose(bool disposing)
101+
{
102+
if (disposed) return;
103+
if (disposing)
104+
{
105+
disposed = true;
106+
foreach (var disposable in disposables)
107+
{
108+
disposable.Dispose();
109+
}
110+
111+
disposables.Clear();
112+
sharedDictionaries.Clear();
113+
}
114+
}
115+
116+
public void Dispose()
117+
{
118+
Dispose(true);
119+
GC.SuppressFinalize(this);
120+
}
109121
}
110122

111123
public static Uri FixDesignTimeUri(Uri inUri)

src/GitHub.VisualStudio.UI/Helpers/ThemeDictionaryManager.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace GitHub.VisualStudio.UI.Helpers
99
{
10-
public sealed class ThemeDictionaryManager : SharedDictionaryManager, IDisposable
10+
public class ThemeDictionaryManager : SharedDictionaryManager, IDisposable
1111
{
1212
static bool isInDesignMode;
1313
Uri baseThemeUri;
@@ -27,18 +27,6 @@ public override Uri Source
2727
}
2828
}
2929

30-
public void Dispose()
31-
{
32-
if (baseThemeUri != null)
33-
{
34-
baseThemeUri = null;
35-
if (!isInDesignMode)
36-
{
37-
VSColorTheme.ThemeChanged -= OnThemeChange;
38-
}
39-
}
40-
}
41-
4230
void InitTheme(Uri themeUri)
4331
{
4432
if (baseThemeUri == null)
@@ -66,5 +54,28 @@ Uri GetCurrentThemeUri()
6654
var currentTheme = Colors.DetectTheme();
6755
return new Uri(baseThemeUri, "Theme" + currentTheme + ".xaml");
6856
}
57+
58+
bool disposed;
59+
private void Dispose(bool disposing)
60+
{
61+
if (disposed) return;
62+
if (disposing)
63+
{
64+
disposed = true;
65+
if (baseThemeUri != null)
66+
{
67+
baseThemeUri = null;
68+
if (!isInDesignMode)
69+
{
70+
VSColorTheme.ThemeChanged -= OnThemeChange;
71+
}
72+
}
73+
}
74+
}
75+
public void Dispose()
76+
{
77+
Dispose(true);
78+
GC.SuppressFinalize(this);
79+
}
6980
}
7081
}

0 commit comments

Comments
 (0)