93
|
1 using System;
|
|
2 using Implab.Parallels;
|
|
3 using System.Threading;
|
|
4
|
|
5 namespace Implab {
|
|
6 public class MTComponentContainer : IComponentContainer, IDisposable {
|
|
7 static readonly MTComponentContainer _appContainer;
|
|
8
|
|
9 static MTComponentContainer() {
|
|
10 _appContainer = new MTComponentContainer();
|
|
11 AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
|
|
12 }
|
|
13
|
|
14 public static MTComponentContainer AppContainer {
|
|
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
|