annotate Implab/MTComponentContainer.cs @ 101:279e226dffdd v2

code cleanup added EnsureDispatched extension
author cin
date Thu, 06 Nov 2014 20:03:19 +0300
parents dc4942d09e74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
1 using System;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
2 using Implab.Parallels;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
3 using System.Threading;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
4
dc4942d09e74 improved tracing
cin
parents:
diff changeset
5 namespace Implab {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
6 public class MTComponentContainer : IComponentContainer, IDisposable {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
7 static readonly MTComponentContainer _appContainer;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
8
dc4942d09e74 improved tracing
cin
parents:
diff changeset
9 static MTComponentContainer() {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
10 _appContainer = new MTComponentContainer();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
11 AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
12 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
13
dc4942d09e74 improved tracing
cin
parents:
diff changeset
14 public static MTComponentContainer AppContainer {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
15 get {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
16 return _appContainer;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
17 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
18 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
19
dc4942d09e74 improved tracing
cin
parents:
diff changeset
20 bool m_disposed;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
21 readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
22
dc4942d09e74 improved tracing
cin
parents:
diff changeset
23 public void Add(IDisposable item) {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
24 Safe.ArgumentNotNull(item, "item");
dc4942d09e74 improved tracing
cin
parents:
diff changeset
25 Thread.MemoryBarrier();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
26 if (m_disposed) {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
27 item.Dispose();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
28 } else {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
29 m_components.Enqueue(item);
dc4942d09e74 improved tracing
cin
parents:
diff changeset
30 if (m_disposed && m_components.TryDequeue(out item))
dc4942d09e74 improved tracing
cin
parents:
diff changeset
31 item.Dispose();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
32 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
33 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
34
dc4942d09e74 improved tracing
cin
parents:
diff changeset
35 public void Dispose() {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
36 m_disposed = true;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
37 IDisposable item;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
38 while (m_components.TryDequeue(out item))
dc4942d09e74 improved tracing
cin
parents:
diff changeset
39 item.Dispose();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
40 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
41
dc4942d09e74 improved tracing
cin
parents:
diff changeset
42 static void HandleProcessExit (object sender, EventArgs e)
dc4942d09e74 improved tracing
cin
parents:
diff changeset
43 {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
44 _appContainer.Dispose();
dc4942d09e74 improved tracing
cin
parents:
diff changeset
45 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
46 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
47 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
48