Event

本文探讨了事件处理机制及多播委托的应用。事件由发布者发送,订阅者接收并处理。多播委托允许多个方法注册到单一事件,实现复杂的响应逻辑。.NET Framework中事件基于EventHandler委托和EventArgs基类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Multicast delegates are used extensively in event handling. Event source objects send event notifications to recipient objects that have registered to receive that event. To register for an event, the recipient creates a method designed to handle the event, then creates a delegate for that method and passes the delegate to the event source. The source calls the delegate when the event occurs. The delegate then calls the event handling method on the recipient, delivering the event data. The delegate type for a given event is defined by the event source.

  • Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisherand the classes that receive (or handle) the event are called subscribersIn a typical C# Windows Forms or Web application, you subscribe to events raised by controls such as buttons and list boxes.

  • Events have the following properties:
    • The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event.
    • An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.
    • Events that have no subscribers are never raised.
    • Events are typically used to signal user actions such as button clicks or menu selections in graphical user interfaces.
    • When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. 
    • In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class.

  • An event is a special kind of multicast delegate that can only be invoked from within the class that it is declared in.  By convention, event delegates in the .NET Framework have two parameters, the source that raised the event and the data for the event.

  • By convention in the .NET Framework, when an event is raised, it passes event data to its event handlers. The event data is provided by theSystem.EventArgs class or by a class that is derived from it. Often, an event has no custom data; the fact that the event was fired provides all of the information that event handlers require. In this case, the event can pass an EventArgs object to its handlers. The EventArgs class has only a single member, Empty, that is not inherited from System.Object. It can be used to instantiate a new EventArgs class. If an event does have custom data, it can pass an instance of a class derived from EventArgs to event handlers. Depending on the precise data the event passes to handlers, you may be able to use an existing event data class in the .NET Framework. For example, if your event handler allows the action associated with the event to be cancelled, you can use the CancelEventArgs class. When you need to provide custom data to handlers and a existing class is not available, you can define your own event data class. It must derive fromSystem.EventArgs. Byconvention, this class is named EventNameEventArgs.

  • The .NET Framework allows subscribers to register for event notifications either statically or dynamically. Static event handlers are in effect for the entire life of the class whose events they handle. This is the most common method of handling events. Dynamic event handlers are explicitly activated and deactivated during program execution, usually in response to some conditional program logic. For example, they can be used if event notifications are needed only under certain conditions or if an application provides multiple event handlers and run-time conditions define the appropriate one to use.

  • If you want your class to raise an event, you must provide the following three elements:
    • A class that provides event data.
    • An event delegate.
    • The class that raises the event.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值