// Wrap event invocations inside a protected virtual method // to allow derived classes to override the event invocation behavior protected virtual void OnTimeElapsedEvent(EventMessageEventArgs e) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. EventHandler <EventMessageEventArgs> handler = this.TimeElapsedEvent; // Event will be null if there are no subscribers if (handler != null) { // Format the string to send inside the CustomEventArgs parameter e.Message += string.Format(" at {0}", DateTime.Now.ToString()); // Use the () operator to raise the event. handler(this, e); } }
private static void ExecuteThisMethod(object sender, EventMessageEventArgs e) { Console.WriteLine(e.Message); }