事件和事件处理程序 [英] Event and event handler

查看:89
本文介绍了事件和事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件和事件处理程序究竟是什么?



在此C#行中



< pre lang =c#> context.BeginRequest + = context_BeginRequest;





事件和事件处理程序在哪里?你如何调用+ =?

解决方案

的左侧和右侧部分到目前为止,它非常简单: context.BeginRequest 是事件实例。这是具有调用列表的类。 '+ ='操作添加了处理程序(实际上是委托实例),这就是调用列表。在您的情况下,处理程序是 context_BeginRequest 。有趣的是,事件实例是 immutable ,因此,实际上,'+ ='运算符创建了一个全新的事件实例实例,并添加了在操作之前已在调用列表中找到的所有处理程序,添加了一个新的handler返回新事件实例。这样,作为引用对象的成员 context.BeginRequestNaturally 将丢失其引用标识。最终,旧的事件实例对象,因为它变得无法访问,将被垃圾收集。这样做是为了改善事件实例的多线程属性。



你可以在我的文章 4.1关于代表实例的性质



现在,正确的部分只是方法,你可以像任何其他方法一样调用它的方法。左边的对象是事件实例。对于事件实例,没有call这样的概念。它可以被调用,因此将调用调用列表的所有元素。这就是没有呼叫的原因。此外,除了声明的类型之外,调用( BeginRequest.Invoke(/*...*/)不能在任何地方完成。如果此类型的源代码是你的,那么你可以调用事件。这是.NET的一个重要的万无一失的功能,可以防止天真的开发人员在不需要时调用事件实例。你可以在我过去的答案中读到它:UserControl自定义事件 [ ^ ]。



-SA


< blockquote>在你要求其他人向你解释.NET和C#面向对象开发的基本概念之前,你至少需要做一些研究,并自己思考......好像你没有被所有人包围您需要在互联网上获得的资源。



从这个优秀的CodeProj开始等文章:[ ^ ]。



替代学习,尝试在Windows窗体上放置一些控件,如Button,TextBox,并创建基本的事件处理程序,例如,对于'Click,或'TextChanged,使用属性浏览器(F4)。



在MSDN上学习本教程:[ ^ ]。



当你替代学习和实验时,想想你的真实世界日常生活中可能与事件相对应的东西,以及一个EventHandler:一个闹钟响起;你的电话响了;一个烟雾报警器?



如果您习惯在C#中为Control事件创建EventHandler,那么继续学习如何创建自己的事件,并了解委托是什么,以及.NET中的Delegates如何实现多播:所有事件订阅者的通知。



并且,考虑以下事实:如果事件有订阅者和订阅者可以添加和删除,然后必须有维护当前订阅者列表的东西。在.NET中,这个待通知事项列表的术语称为调用列表:,特殊函数+ =和 - =添加和删除事件的订阅者。在C#中,您对此调用列表的访问权限非常有限(其他语言提供不同类型的访问权限)。



并且,因为您具有特定的问题,最好包括你自己编写的代码:回到这里问他们。


What exactly is an event and an event handler?

In this C# line

context.BeginRequest += context_BeginRequest;



Where is the event and the event handler? How do you call the left part and and right part of the +=?

解决方案

So far, it's pretty simple: context.BeginRequest is the event instance. This is the class which has invocation list. The '+=' operation adds the handler (actually, a delegate instance), this that invocation list. In your case, the handler is context_BeginRequest. Interestingly, event instances are immutable, so, in fact, '+=' operator creates a brand new instance of the event instance and adds all handlers already found in the invocation list before the operation, adds a new handler are returns that new event instance. This way, the member context.BeginRequestNaturally, which is the reference object, looses its referential identity. Eventually, old event instance object, as it becomes inaccessible, will be garbage-collected. This is done to improve multithreading properties of event instances.

You can read about it, as well as some other interesting facts, in my article, the section 4.1 On the Nature of Delegate Instance.

Now, as right part is just the method, you can call it method the same way as any other method. The object on left is the event instance. For event instances, there is no such concept as "call". It can be invoked, so all elements of the invocation list will be called. That's why there is no a "call". Moreover, the invocation (BeginRequest.Invoke(/*...*/) cannot be done anywhere except the declared type. If the source code of this type is yours, you can invoke the event. This is the important fool-proof feature of .NET, to guard naive developers from invoking an event instance when it is not needed. You can read about it in my past answer: UserControl custom event[^].

—SA


You need to do at least a minimum of research, and thinking for yourself, before you ask other people to explain fundamental concepts in .NET and C# Object-Oriented-Development to you ... as if you are not surrounded by all the resources you need on the internet.

Start with this excellent CodeProject article: [^].

Alternate studying with experimenting with putting some Controls, like a Button, a TextBox, on a Windows Forms, and create basic Event Handlers, for example, for 'Click, or 'TextChanged, using the Property Browser (F4).

Study this tutorial on MSDN: [^].

As you alternate study and experimentation, think about what in your "real world" daily life might correspond to an Event, and an EventHandler: an alarm clock going off; your telephone ringing; a smoke alarm ?

When you are comfortable creating EventHandlers for Control events in C#, then move on to learning how to create your own Event, and understanding what a Delegate is, and how Delegates in .NET implement multi-casting: the notification of all Event "subscribers."

And, think about the fact that: if an Event has "subscribers," and subscribers can be added, and deleted, then there must be something that "maintains" a list of the current subscribers. In .NET the term for this list of things-to-be-notified is called the "invocation list:" and the special functions += and -= add, and remove, a subscriber to the Event. In C# you have very limited access to this invocation list (other languages offer different types of access).

And, as you have specific questions, preferably involving code you have written yourself: come back here and ask them.


这篇关于事件和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆