Mercurial > pub > ImplabNet
diff Implab/MTComponentContainer.cs @ 93:dc4942d09e74 v2
improved tracing
added the application components container MTComponentContainer.AppContainer
author | cin |
---|---|
date | Thu, 23 Oct 2014 01:13:57 +0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/MTComponentContainer.cs Thu Oct 23 01:13:57 2014 +0400 @@ -0,0 +1,48 @@ +using System; +using Implab.Parallels; +using System.Threading; + +namespace Implab { + public class MTComponentContainer : IComponentContainer, IDisposable { + static readonly MTComponentContainer _appContainer; + + static MTComponentContainer() { + _appContainer = new MTComponentContainer(); + AppDomain.CurrentDomain.ProcessExit += HandleProcessExit; + } + + public static MTComponentContainer AppContainer { + get { + return _appContainer; + } + } + + bool m_disposed; + readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>(); + + public void Add(IDisposable item) { + Safe.ArgumentNotNull(item, "item"); + Thread.MemoryBarrier(); + if (m_disposed) { + item.Dispose(); + } else { + m_components.Enqueue(item); + if (m_disposed && m_components.TryDequeue(out item)) + item.Dispose(); + } + } + + public void Dispose() { + m_disposed = true; + IDisposable item; + while (m_components.TryDequeue(out item)) + item.Dispose(); + } + + static void HandleProcessExit (object sender, EventArgs e) + { + _appContainer.Dispose(); + } + } +} +