In all the introduction to the C # event mechanism, I prefer the publisher/Subscriber (Publisher/subscriber) description. Understanding the event mechanism is not an easy task, and the thought involved is worthy of our good study.
This resource comes from the "C # and. NET Technology platform Practical walkthrough"--China Youth Press
When it comes to events, we involve two roles: the event publisher (publisher) and the event Subscriber (Scriber), which can also be said to be the relationship between the sender of the event and the receiver of the event. For example, there are a lot of magazines and magazines on the market. And I'm only interested in some of them, so I can offer a subscription to a magazine publisher. Then, whenever the magazine was released, I received the magazine I subscribed to in the magazine publisher. In this relationship, the magazine publisher is equivalent to the event issuer, and I am the event subscriber. Whenever a magazine is released, a release event is triggered.
In object-oriented language interpretation, the meanings of both are as follows:
Event Publisher (publisher)
It is an object and maintains its own state information. Whenever the state information changes, an event is triggered and all event subscribers are notified. For magazine publishers, every magazine has its own information in it, and when the magazine is published, I want to notify the person who subscribed to the magazine: The magazine has been issued, please pay attention to check!
Event Receiver (receiver)
This object wants to register its object of interest, which is to subscribe to its favorite magazine. In addition, this object typically provides an event-handling method that is automatically executed after the event Publisher triggers an event. For the example above, which is what I'm going to do when I get a magazine, for example, you can shout all over the place: I got the magazine! You can also collect the magazine slowly appreciate, how to achieve exactly depends on your own preferences.
Here is the. NET event-handling mechanism model:
Description: Multiple event handlers can be registered
C # event mechanism