I'm really embarrassed to ask such a trivial question but debugging some software now convinced me I don't really understand this issue:
How does .NET events work seen from an altitude of 20,000 feet? I don't mean the delegate/event handler pattern and all this. What I mean is - what's the BIG picture:
- Code A is doing something.
- Some external trigger happens. Say, for example, that the user clicked on some control.
- A magic happens and the event handler for the event is called.
- another magic happens after the event handler returns.
Now, what is the magic? How does this relate to threads? Is the thread running my code interrupted when the event occurs, and then resumes after the event handler return? But I googled and found out that .NET handlers are called synchronously in the original thread. So who takes care of stopping and resuming Code A? What happens if events are nested (i.e. Event 2 happens when the event handler for Event 1 is running)?
Edit: As far as I understand the answers say that the event hander for the next event will run only after the currently running event handler finishes. This means that your code is not interrupted: line n will always run immediately just after line n-1 and just before line n+1. However just before I posted the question I was debugging a program controlling, through automation, Internet Explorer (using SWExplorerAutomation from Webius). I'm quite sure that as I was line-stepping through the code I was "kidnapped" :-) to some event handler and returned to the interrupted position in the code once that event handler finished its business. This means that either do not understand the answers, or that the program behaves differently while being stepped through the debugger!