.NET Event Engine

In all books on C # /. NET, which I have seen so far, when they talk about events,
they talk about the events of Creation and Consumption .

I am curious to understand a little how this works for our code - what is the mechanism for its launch.

I understand a little that Windows Message Loop acts as a queue for events that occur.
For example, WM_KEYDOWNor WM_LBUTTONDOWN, etc.

But what happens, for example, if I create a class that does not inherit class Control, and this class raises an event?
(which gets another class that doesn't inherit either class Control)

Will the raised event go through the message loop?
It doesn’t sound so logical ..
(however, suppose the project is a Windows Forms project, only 2 classes - the sender and the receiver are not graphic classes at all, but simple classes that you wrote)

Any explanation or link to an article about the mechanism of our code would be highly appreciated.

+4
source share
1 answer

I hope I understand your question correctly. I think we are talking about two things.

Firstly, how events work in C # Secondly, how a WinForms application written in C # knows when you clicked a button.

# - . , , , , ..... . Add(), , Remove(). , , , .

: # ?

- #. , Win32, . Winforms, , ? ( " " https://msdn.microsoft.com/en-us/library/dn457346.aspx) , , .

Callstack

, Windows.Forms.Controls.ControlNativeWindow WndProc, System.Windows.Forms.Message m.

, "debuggableCallback". , Win32API.

: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NativeWindow.cs,ad40308c5b6490dd

/// <include file='doc\NativeWindow.uex' path='docs/doc[@for="NativeWindow.DebuggableCallback"]/*' />
/// <devdoc>
///     Window message callback method. Control arrives here when a window
///     message is sent to this Window. This method packages the window message
///     in a Message object and invokes the wndProc() method. A WM_NCDESTROY
///     message automatically causes the releaseHandle() method to be called.
/// </devdoc>
/// <internalonly/>
private IntPtr DebuggableCallback(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) {

    // Note: if you change this code be sure to change the 
    // corresponding code in Callback above!

    Message m = Message.Create(hWnd, msg, wparam, lparam);

    try {
        if (weakThisPtr.IsAlive && weakThisPtr.Target != null) {
            WndProc(ref m);
        }
        else {
            DefWndProc(ref m);
        }
    }
    finally {
        if (msg == NativeMethods.WM_NCDESTROY) ReleaseHandle(false);
        if (msg == NativeMethods.WM_UIUNSUBCLASS) ReleaseHandle(true);
    }

    return m.Result;
}

, , Windows, API Win32, . , System.Windows.Forms , .

+4

Source: https://habr.com/ru/post/1621878/


All Articles