Return to Snippet

Revision: 29987
at August 6, 2010 01:04 by jmillerit


Initial Code
    public class SingletonClassName
    {
        #region EventArg Declarations

        public class SingletonClassNameEventArgs : EventArgs
        {
            #region Properties

            public PropertyDataType PropertyName { get; set; }

            #endregion



            #region Constructors

            public SingletonClassNameEventArgs(PropertyDataType _PropertyName)
            {
                this.PropertyName = _PropertyName;
            }

            #endregion
        }

        #endregion



        #region Variable Declarations

        private static readonly SingletonClassName _instance = new SingletonClassName();
        private static object _threadSynchronizationHandle = null;

        #endregion



        #region Constructors

        //Note that we are making the constructor private.  Doing so ensures that the class cannot
        //be directly instantiated which is one of the principles of the Singleton pattern.
        private SingletonClassName()
        {
        }

        #endregion



        #region Singleton Access Point

        public static SingletonClassName Instance
        {
            get
            {
                lock (GetThreadSynchronizationHandle())
                {
                    return _instance;
                }
            }
        }

        #endregion



        #region Thread Synchronization Handles

        private static object GetThreadSynchronizationHandle()
        {
            //When the thread synchronization handle object is requested, we use the CompareExchange
            //method of the Interlocked class to see if the value is null.  If it is null, then we 
            //will create the new object.  If it is not null then we will return the previously
            //allocated lock target.
            Interlocked.CompareExchange(ref _threadSynchronizationHandle, new object(), null);
            return _threadSynchronizationHandle;
        }

        #endregion



        #region Public Event Handlers

        public event EventHandler<SingletonClassNameEventArgs> OnRaiseEventNameEvent;

        #endregion



        #region Public Raise Event Handlers

        public void RaiseEventNameEvent(SingletonClassNameEventArgs e)
        {
            //Note that we are making a temporary copy of the event to avoid the possibility of a race 
            //condition if the last subscriber of the event unsubscribes immediately after
            //the null check and before the actual event is raised.  
            EventHandler<SingletonClassNameEventArgs> eventHandler = OnRaiseEventNameEvent;



            //We will only raise the event if there are subscribers to the event.
            if (eventHandler != null)
            {
                eventHandler(this, e);
            }
        }

        #endregion



        #region Public Methods

        #endregion



        #region Private Methods

        #endregion
    }

Initial URL

                                

Initial Description

                                

Initial Title
Singleton With Event Handler

Initial Tags

                                

Initial Language
C#