forked from sharpdx/SharpDX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisposeBase.cs
More file actions
75 lines (65 loc) · 2.48 KB
/
Copy pathDisposeBase.cs
File metadata and controls
75 lines (65 loc) · 2.48 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
namespace SharpDX
{
/// <summary>
/// Base class for a <see cref="IDisposable"/> class.
/// </summary>
public abstract class DisposeBase : IDisposable
{
/// <summary>
/// Occurs when this instance is starting to be disposed.
/// </summary>
public event EventHandler<EventArgs> Disposing;
/// <summary>
/// Occurs when this instance is fully disposed.
/// </summary>
public event EventHandler<EventArgs> Disposed;
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="DisposeBase"/> is reclaimed by garbage collection.
/// </summary>
~DisposeBase()
{
// Finalizer calls Dispose(false)
CheckAndDispose(false);
}
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
public bool IsDisposed { get; private set; }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
CheckAndDispose(true);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
private void CheckAndDispose(bool disposing)
{
// TODO Should we throw an exception if this method is called more than once?
if (!IsDisposed)
{
EventHandler<EventArgs> disposingHandlers = Disposing;
if (disposingHandlers != null)
disposingHandlers(this, EventArgs.Empty);
Dispose(disposing);
GC.SuppressFinalize(this);
IsDisposed = true;
EventHandler<EventArgs> disposedHandlers = Disposed;
if (disposedHandlers != null)
disposedHandlers(this, EventArgs.Empty);
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected abstract void Dispose(bool disposing);
}
}