We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is an Event Loop?

By Alex Newth
Updated: May 16, 2024

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.

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.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
By Logicfest — On Feb 27, 2014

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?

Share
https://www.easytechjunkie.com/what-is-an-event-loop.htm
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.