Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

The Super Pool Framework

Rate me:
Please Sign up or sign in to vote.
4.87/5 (53 votes)
31 Aug 2010CPOL26 min read 101.4K   1.5K   178  
The Super Pool is a framework for decoupled communication and management of components. The Super Pool introduces a natural asynchronous communication environment into your solution that can be fluently spread over different components, threads, processes, or even computers or networks.
// -----
// Copyright 2010 Deyan Timnev
// This file is part of the Matrix Platform (www.matrixplatform.com).
// The Matrix Platform is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, 
// either version 3 of the License, or (at your option) any later version. The Matrix Platform is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
// without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License along with the Matrix Platform. If not, see http://www.gnu.org/licenses/lgpl.html
// -----
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using Matrix.Common.Core;

namespace Matrix.Common.Core
{
    /// <summary>
    /// Helps with the control and events of lifetime events of the application.
    /// In console applications make sure to call "SetApplicationClosing" upon close.
    /// In ApplicationDomains mode - tested under nunit and handled.
    /// In Service mode - not tested.
    /// </summary>
    public static class ApplicationLifetimeHelper
    {
        static System.Threading.Mutex _applicationMutex = null;

        static volatile Stopwatch _applicationStopwatch = null;
        /// <summary>
        /// The stopwatch measures time since the application was started.
        /// </summary>
        public static long ApplicationStopwatchTicks
        {
            get
            {
                return _applicationStopwatch.ElapsedTicks;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static long ApplicationStopwatchMilliseconds
        {
            get
            {
                return _applicationStopwatch.ElapsedMilliseconds;
            }
        }

        /// <summary>
        /// CompletionEvent raised when application closing. Preffered to use this instead of Application.ApplicationExit
        /// since a Windows Service implementation may require modifications.
        /// </summary>
        public static event CommonHelper.DefaultDelegate ApplicationClosingEvent;

        volatile static bool _applicationClosing = false;
        /// <summary>
        /// 
        /// </summary>
        public static bool ApplicationClosing
        {
            get
            {
                //Console.
                if (_applicationClosing && Environment.HasShutdownStarted)
                {// A shutdown has started.
                    _applicationClosing = false;
                }

                return _applicationClosing;
            }
        }

        /// <summary>
        /// Static constructor.
        /// </summary>
        static ApplicationLifetimeHelper()
        {
            _applicationStopwatch = new Stopwatch();
            _applicationStopwatch.Start();

            //System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
            //process.Exited += new EventHandler(process_Exited);

            // This greatly helps when the module is loaded inside an AppDomain (like in nUnit).
            AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);

            // This is a soft connection to the Application class, and will operate only 
            // when it is available in the using application.
            Type application = Type.GetType("System.Windows.Forms.Application, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            if (application != null)
            {
                EventInfo exitEventInfo = application.GetEvent("ApplicationExit");
                if (exitEventInfo != null)
                {
                    exitEventInfo.AddEventHandler(null, new EventHandler(Application_ApplicationExit));
                }
            }
        }

        static void CurrentDomain_DomainUnload(object sender, EventArgs e)
        {
            SetApplicationClosing();
        }

        /// <summary>
        /// Creates a static application mutex, that is usefull for 
        /// checking if the application is already running.
        /// </summary>
        /// <returns>Returns false this call fails (already called this before), or true if it is good.</returns>
        public static bool TryGetApplicationMutex(string mutexName, out bool createdNew)
        {
            createdNew = false;
            if (_applicationMutex != null)
            {
                return false;
            }

            _applicationMutex = new System.Threading.Mutex(true, mutexName, out createdNew);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public static void ReleaseApplicationMutex()
        {
            if (_applicationMutex != null)
            {
                _applicationMutex.Close();
                _applicationMutex = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void SetApplicationClosing()
        {
            if (_applicationClosing == false)
            {
                _applicationClosing = true;

                CommonHelper.DefaultDelegate delegateInstance = ApplicationClosingEvent;
                if (delegateInstance != null)
                {
                    delegateInstance();
                }
            }
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            SetApplicationClosing();

            //// On closing the application, release the threadpool resources and threads to speed up closing.
            //// _threadPoolEx.Dispose();

            //_applicationClosing = true;

            //if (ApplicationClosingEvent != null)
            //{
            //    ApplicationClosingEvent();
            //}
        }

        //static void process_Exited(object sender, EventArgs e)
        //{
        //    int h = 2;
        //}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Ingenious Ltd, Bulgaria
Bulgaria Bulgaria
I worked for a few years as a C++/Win32 developer and software architect, and then moved on to the .NET environment where I was able to discover the beauty of managed programming.

I am currently involved in the development and management of Open Forex Platform (www.openforexplatform.com) and the Matrix Platform (www.matrixplatform.com).

Comments and Discussions