简体   繁体   中英

How is an event raised in C#?

My understanding about events in C# for a console application:

  1. create a delegate that has the same signature as the event handler method in the subscriber class.

  2. declare an event based on this delegate

  3. raise the event
    My understanding of this step is: it is simply an wrapper function where parameters are passed to the event to invoke the event handler functions pointed to by the event.

So raising the event is just invoking the wrapper function in the publish class.

Now when I create a very simple Windows form application, I am not able to apply this general concept.

Consider a WinForms application with just one button.

// registering statement
Button1.Click += new EventHandler (this.button1_click)
  1. I can identify the first step. It is the pre-defined System.EventHandler delegate.

  2. Click event for the button is also pre-defined. No problem with that.

  3. event raising step : here I fail to make the connection between a console application and an Windows application.

Where is the code kept that actually RAISES the event for a WinForms application? We don't seem to have to code it.

I understand click event is raised when someone "clicks" on the button, but how is that realized in the C# code for WinForms application?

How does the compiler "just" knows that a Click event for a button means someone clicking on a button and therefore an event should be raised?

How is click event raised? How are the parameters passed to the event?

The Control class has protected function called WndProc , when the OS needs to tell the program something it generates a Message object and passes it in to the WndProc function.

That WndProc function then looks at the message and sees what kind of message it is, if it is the " mouse left button up " message it runs the the OnClick method with the correct parameters parsed out of the Message object that was passed in to WndProc .

That OnClick method is the thing that raises the event to the subscriber.

From the soruce of .NET:

Your understanding is a bit backwards. I think this is why you have issues.

You are not creating a delegate that has the same signature as the event handler method in the subscriber class.

What you are doing is declaring what a function to which to delegate execution will look like. Here is the signature for EventHandler:

public delegate void EventHandler(object sender, EventArgs e)

So, if you want a function to be able to handle delegation of the event, it must follow that signature.

Now, the class that will delegate execution to subscribers needs a reference to those functions so it can call them when the event takes place. That is why you implement an event property. It follows then that the Button class must expose this property for you to be able to "hook" your delegates:

public event EventHandler Click

(Notice this is inherited from Control)

When you register an "event handler":

Button1.Click += new EventHandler (this.button1_click)

You are essentially saying that you want this.button1_click(object sender, EventArgs e) to fire whenever the Click event is raised by the Button1 instance.

The Button1 instance will internally decide when to fire the event at which point it will use the event property to delegate execution to the subscribed functions. It will call them with the above mentioned parameters where sender will most likely be the instance itself and the EventArgs class will give you additional information about the conditions that raised the event. The property is also usually implemented to add additional checks (like if there is anything to call in the first place).

As you can see, the code that actually raises the click is internal to the implementation of the Button (or its inheritance chain). It obviously involves mouse tracking and what not, which is the benefit of using the controls by the way, unless you want to write all that detection stuff from scratch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM