annotate Implab/Disposable.cs @ 40:fe33f4e02ad5

improved tracing added text listeners (file,console)
author cin
date Tue, 15 Apr 2014 17:52:09 +0400
parents
children d9d794b61bb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Diagnostics;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5 using System.Web;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 namespace Implab {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8 public class Disposable : IDisposable {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
10 bool m_disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 public event EventHandler Disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 public bool IsDisposed {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15 get { return m_disposed; }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
17
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
18 protected void AssertNotDisposed() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19 if (m_disposed)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20 throw new ObjectDisposedException(this.ToString());
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23 protected virtual void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24 if (disposing && !m_disposed) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 m_disposed = true;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
26
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
27 EventHandler temp = Disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28 if (temp != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 temp(this,EventArgs.Empty);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32 public void Dispose() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33 Dispose(true);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
34 GC.SuppressFinalize(this);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 protected virtual void ReportObjectLeaks() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 Trace.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 ~Disposable() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42 Dispose(false);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43 ReportObjectLeaks();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
45 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
46 }