Internet
Fact-checked

At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What Is an Event Loop?

Alex Newth
Alex Newth

An event loop — or main loop, if it is the central control aspect — is a construct within programs that controls and dispatches events following an initial event. The initial event can be anything, including pushing a button on a keyboard or clicking a button on a program. After polling the devices, the event loop will dispatch an event that creates a desired effect, based on the programming. This is called a loop, not because the event circles and happens continuously, but because the loop prepares for an event, checks the event, dispatches an event and repeats the process all over again.

In computer programming, there is a structure called the event loop. This is a block of code that tells the computer to wait for an event and what to do in response to the event. Sometimes the loop only takes up a small portion of coding; more often, it is the central logic of a coding document. When it makes up the central logic, it also is called the main loop.

Man holding computer
Man holding computer

The event for which an event loop waits can be anything. An event can manifest from a user pressing a keyboard button, moving a mouse, opening an application, or anything else a user can do with a computer. The event usually comes from an outside device, such as the keyboard or mouse, but it also can be a result of internal processes. When the specified event happens, the main loop will trigger another event in response, such as posting a letter in a document or allowing the user to access a folder.

Four processes are needed to create the event loop. The first event is called the prepare query. This prepares the program to poll the device or devices responsible for the event, so the computer gets the proper resources allocated for the action. After this, the loop actually polls devices for the event to see if the event occurred. Last in this order is the check function, which returns the result of the poll to the loop.

If the event returns as true, then the loop continues; if not, it goes back to prepare. If true, the fourth action — called dispatch — occurs. This is when all the resources are dispatched to create a response to the polled event, causing a reaction. After dispatch, the loop goes back to prepare, to ready itself again to check for events.

Discussion Comments

Logicfest

Aren't there times when an event loop becomes an actual, annoying loop? For example, there are times when one shuts down an operating system such as Microsoft Windows and the process gets "stuck" preparing to shut down the OS. Similarly, there are times when an operating system will get "stuck" when powered up and the computer won't boot.

Are these never ending loops related to the event loop discussed in the article and, if so, how can one diagnose what is happening?

Post your comments
Login:
Forgot password?
Register:
    • Man holding computer
      Man holding computer