Singleton With Event Handler


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public class SingletonClassName
  2. {
  3. #region EventArg Declarations
  4.  
  5. public class SingletonClassNameEventArgs : EventArgs
  6. {
  7. #region Properties
  8.  
  9. public PropertyDataType PropertyName { get; set; }
  10.  
  11. #endregion
  12.  
  13.  
  14.  
  15. #region Constructors
  16.  
  17. public SingletonClassNameEventArgs(PropertyDataType _PropertyName)
  18. {
  19. this.PropertyName = _PropertyName;
  20. }
  21.  
  22. #endregion
  23. }
  24.  
  25. #endregion
  26.  
  27.  
  28.  
  29. #region Variable Declarations
  30.  
  31. private static readonly SingletonClassName _instance = new SingletonClassName();
  32. private static object _threadSynchronizationHandle = null;
  33.  
  34. #endregion
  35.  
  36.  
  37.  
  38. #region Constructors
  39.  
  40. //Note that we are making the constructor private. Doing so ensures that the class cannot
  41. //be directly instantiated which is one of the principles of the Singleton pattern.
  42. private SingletonClassName()
  43. {
  44. }
  45.  
  46. #endregion
  47.  
  48.  
  49.  
  50. #region Singleton Access Point
  51.  
  52. public static SingletonClassName Instance
  53. {
  54. get
  55. {
  56. lock (GetThreadSynchronizationHandle())
  57. {
  58. return _instance;
  59. }
  60. }
  61. }
  62.  
  63. #endregion
  64.  
  65.  
  66.  
  67. #region Thread Synchronization Handles
  68.  
  69. private static object GetThreadSynchronizationHandle()
  70. {
  71. //When the thread synchronization handle object is requested, we use the CompareExchange
  72. //method of the Interlocked class to see if the value is null. If it is null, then we
  73. //will create the new object. If it is not null then we will return the previously
  74. //allocated lock target.
  75. Interlocked.CompareExchange(ref _threadSynchronizationHandle, new object(), null);
  76. return _threadSynchronizationHandle;
  77. }
  78.  
  79. #endregion
  80.  
  81.  
  82.  
  83. #region Public Event Handlers
  84.  
  85. public event EventHandler<SingletonClassNameEventArgs> OnRaiseEventNameEvent;
  86.  
  87. #endregion
  88.  
  89.  
  90.  
  91. #region Public Raise Event Handlers
  92.  
  93. public void RaiseEventNameEvent(SingletonClassNameEventArgs e)
  94. {
  95. //Note that we are making a temporary copy of the event to avoid the possibility of a race
  96. //condition if the last subscriber of the event unsubscribes immediately after
  97. //the null check and before the actual event is raised.
  98. EventHandler<SingletonClassNameEventArgs> eventHandler = OnRaiseEventNameEvent;
  99.  
  100.  
  101.  
  102. //We will only raise the event if there are subscribers to the event.
  103. if (eventHandler != null)
  104. {
  105. eventHandler(this, e);
  106. }
  107. }
  108.  
  109. #endregion
  110.  
  111.  
  112.  
  113. #region Public Methods
  114.  
  115. #endregion
  116.  
  117.  
  118.  
  119. #region Private Methods
  120.  
  121. #endregion
  122. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.