116
|
1 using System;
|
|
2 using Implab.Parallels;
|
|
3 using System.Threading;
|
|
4
|
|
5 namespace Implab {
|
|
6 public class ComponentContainer : IComponentContainer, IDisposable {
|
|
7 static readonly ComponentContainer _appContainer;
|
|
8
|
|
9 static ComponentContainer() {
|
|
10 _appContainer = new ComponentContainer();
|
|
11 AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
|
|
12 }
|
|
13
|
|
14 public static ComponentContainer Global {
|
|
15 get {
|
|
16 return _appContainer;
|
|
17 }
|
|
18 }
|
|
19
|
|
20 bool m_disposed;
|
|
21 readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>();
|
|
22
|
|
23 public void Add(IDisposable item) {
|
|
24 Safe.ArgumentNotNull(item, "item");
|
|
25 Thread.MemoryBarrier();
|
|
26 if (m_disposed) {
|
|
27 item.Dispose();
|
|
28 } else {
|
|
29 m_components.Enqueue(item);
|
|
30 if (m_disposed && m_components.TryDequeue(out item))
|
|
31 item.Dispose();
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public void Dispose() {
|
|
36 m_disposed = true;
|
|
37 IDisposable item;
|
|
38 while (m_components.TryDequeue(out item))
|
|
39 item.Dispose();
|
|
40 }
|
|
41
|
|
42 static void HandleProcessExit (object sender, EventArgs e)
|
|
43 {
|
|
44 _appContainer.Dispose();
|
|
45 }
|
|
46 }
|
|
47 }
|
|
48
|