-
Notifications
You must be signed in to change notification settings - Fork 4
/
StaticFinalizer.cs
49 lines (45 loc) · 1.63 KB
/
StaticFinalizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
namespace GeoFramework
{
/// <summary>
/// Serves as a notification to dispose of static objects.
/// </summary>
/// <remarks>It is not uncommon for static variables to contain unmanaged resources. Yet,
/// the .NET Garbage Collector does not allow for finalizers on static objects. The StaticFinalizer
/// class serves to work around this problem. To use this class, declare an instance as a
/// private, static variable. Then, hook into its Disposed event. The event will be raised
/// during the StaticFinalizer's own finalizer, allowing you to safely dispose of static resources.</remarks>
/// <example lang="cs">
/// private static StaticFinalizer MyStaticFinalizer = new StaticFinalizer();
/// private static Brush UnmanagedResource = new SolidBrush(Color.Blue);
///
/// void Constructor()
/// {
/// MyStaticFinalizer.Disposed += new EventHandler(StaticFinalize);
/// }
///
/// void StaticFinalize(object sender, EventArgs e)
/// {
/// UnmanagedResource.Dispose();
/// }
/// </example>
public class StaticFinalizer
{
public event EventHandler Disposed;
public static readonly StaticFinalizer Current = new StaticFinalizer();
public StaticFinalizer()
{ }
~StaticFinalizer()
{
try
{
if (Disposed != null)
Disposed(this, EventArgs.Empty);
}
catch
{
// Finalizers can't throw exceptions
}
}
}
}