Mercurial > pub > ImplabNet
annotate Implab/ComponentContainer.cs @ 143:16f926ee499d v2
DRAFT: refactoring, adding cancelation token
author | cin |
---|---|
date | Wed, 04 Mar 2015 18:05:39 +0300 |
parents | 2573b562e328 |
children |
rev | line source |
---|---|
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; | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
116
diff
changeset
|
21 readonly AsyncQueue<IDisposable> m_components = new AsyncQueue<IDisposable>(); |
116 | 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 |