Asynchronous and .NET events?

I am very ashamed to ask such a trivial question, but debugging some software has now convinced me that I really do not understand this problem:

How do .NET events work from 20,000 feet? I don't mean the delegate / event handler pattern and all that. I mean - what a BIG picture:

  • Code A does something.
  • Some external trigger is executed. Say, for example, that the user clicked on some control.
  • The magic happens , and the event handler for the event is called.
  • one more magic will happen after the event handler returns.

Now what is magic? How does this relate to threads? Is the thread executing my code interrupted when an event occurs and then resumes after the event handler returns? But I googled and found out that .NET handlers are called synchronously in the source stream. So who cares about terminating and resuming code A? What happens if events are nested (i.e., Event 2 occurs when the event handler for event 1 is executed)?

Change . As far as I understand, the answers say that the event handler for the next event will be launched only after the current start handler is completed. This means that your code is not interrupted: line n always starts immediately after line n-1 and immediately before line n + 1. However, before I posted the question, I debugged the program that controls, through automation, Internet Explorer (using SWExplorerAutomation from Webius). I am quite sure that as I was hatching the code, I was "kidnapped" :-) for some event handler and returned to the interrupted position in the code after the event handler completed its work. This means that either the answers are not understood, or that the program behaves differently when it passes through the debugger!

+5
3

. - Windows. , , A , . , .

A, . , . A , Click, .

. , . , , .

+3

, 20 000 , - MessageLoop. Application.Run().

, while,

  // pseudo code, I did not reflector Application.Run
  while (GetMessage(ref msg)
  {
     DispatchMessage(ref msg);
  }

, 1 simgle, "" TaskManager.

Application.DoEvents(), .

+2

- ( , ++). .NET, , + = . , 20 000 - , . .

WinForms/WPF : , , .

, , , ( ) .

, - , , . , , ( ).

+1

All Articles