display | more...

An "event loop" is what you might call the "nerve center" of an "event-driven" computer program.

In plain English, an "event-driven" program is one that usually just sits with its hands folded, waiting for something to happen. This is the way interactive programs tend to work. Odds are you're using just such a program right now: You're sitting and reading. What's your web browser doing while you read? It's not doing a damn thing. It's waiting.

Watching, and waiting.

When you hit a key or click the mouse on something, that's an "event". There are many kinds of "events", but we'll just worry about those simple ones for now.

When this key-pressing "event" happens, the operating system finds out about it from your hardware, and it tells the program. The exact details vary a bit, but on the whole, it works something like this. To keep things simple, we'll imagine that our "program" is a little game where you use the arrow keys to move a spaceship around on the computer screen. If you press the space bar, the little spaceship explodes and you lose. Just like real life.

    Did the operating system tell us that something happened?
            Go back to START_HERE and start over.
            Did the user press a key?
                    Go back to START_HERE.
                    If it was the down-arrow key,
                        Move the spaceship up.
                    If it was the up-arrow key,
                        Move the spaceship down.
                    If it was the left-arrow key,
                        Move the spaceship to the left.
                    If it was the right-arrow key,
                        Move the spaceship to the right.
                    If it was the space bar,
                        BLAMMO! YOU LOSE! HAHAHAHAAA!
                        STOP THE PROGRAM! THE END!
                    If it wasn't any of those,
                        Go back to START_HERE.

So that's our "event loop": It loops around and around. If you hit the up-arrow key, it moves the spaceship up and then starts over, looking for another "event". If you click the mouse, it goes right back to the beginning because it doesn't have any plans for that "event". This goes on and on like an endless nightmare until you press the spacebar and lose the game.

That was a very simple event loop, as these things go. In a regular Windows program (or a Mac program, I'd imagine, though I haven't written one and am not familiar with the details), it could be very complicated indeed. In a program of any size, the event loop will hand off all of the details to other bits of code, to keep itself down to a manageable size. All it does is hand out assignments: "Okay, that was the mouse -- hey, you, Ms. Mouse-Handling Code, deal with this. (brief twiddling of thumbs) What's this? He's quitting the program? Mr. Program-Quitter Code, please attend to this matter ASAP." And so on. When you look at your computer, imagine a thousand tiny middle managers officiously handing out assignments to bored underlings, and you won't be too far wrong.

The point is that everything the program does, it does in response to some "event", which sends it off on some "branch" of the code in the event loop. The event loop just keeps looping until an "event" happens where the instruction for that event says to quit the program.

If you know a little bit about programming, you can see a live example of the real thing at Win32 system tray icon: Just search the page for "WindowProc". That's the guts of the event loop right there. The actual loop happens elsewhere, but "WindowProc" is the part that mostly does stuff. A real Windows program doesn't actually spin like a busy loop the way the one above does; that would be horribly wasteful.

Log in or register to write something here or to contact authors.