0

Consider i have a "Form" and it contains Button. I started the compiling process (Ctrl +F5). When i click the button the event is occurs and event handler is executed. Where does execution come back after event is handled?

 public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //some Code
    }
}
Mahmood Garibov
  • 314
  • 2
  • 13
  • 2
    Th execution comes back to the UI thread – Chetan Apr 01 '20 at 13:12
  • 1
    Use the debugger, look at the Stack Trace window. You'll see button1_Click at the top, Application.Run() and Main() at the bottom. In between are the methods you didn't write or call yourself, inside Winforms that take care of handling the operating system notification and getting the Click event to fire. Application.Run() is the crucial call, that gets all of the plumbing to work. – Hans Passant Apr 01 '20 at 13:24

3 Answers3

2

Short Answer: It goes back to listening for more events.

Detailed Answer:

Under the hood, everything in Windows runs on top of the Win32 API. The Win32 API has at least 2 functions that all programs run. The window procedure is one and that is where our event messages get processed. The other one is called the message loop and it looks similar to this:

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
return Msg.wParam;

The message loop is the heart of all event-based Windows programs. GetMessage() gets a message from your application's message queue. Any time the user moves the mouse, types on the keyboard, clicks on your window's menu, or does any number of other things, messages are generated by the system and entered into your program's message queue. By calling GetMessage() you are requesting the next available message to be removed from the queue and returned to you for processing.

TranslateMessage() does some additional processing on keyboard events. Finally DispatchMessage() sends the message out to the window that the message was sent to.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
1

In every winforms application is "hidden" main message loop. Main message loop is basicaly while(true) ... loop and checks for input (and some other) events like Click, KeyDown, etc...

This main loop is executed inside Application.Run(...) is call. Probably in your Program.cs file.

TcKs
  • 25,849
  • 11
  • 66
  • 104
1

Check the Events overview for Windows Forms

When an event is recorded by the application, the control raises the event by invoking the delegate for that event. The delegate in turn calls the bound method. In the most common case (a multicast delegate) the delegate calls each bound method in the invocation list in turn, which provides a one-to-many notification. This strategy means that the control does not need to maintain a list of target objects for event notification—the delegate handles all registration and notification.

So the control itself, calls the delegate. The code of the control raised the method. So when you click the control there is some code in the control that calls the delegate you have provided.

The code commonly calls the delegate as it would any other function and continuous it's code flow:

i.e.

// do something
deletat(...);
// continue doing something

You can find an even better example here: Understanding events and event handlers in C#

on how delegates are being called.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61