Main loop |
In Computing, the main loop (sometimes called the event loop or main event loop) is a common design approach, typically used by applications featuring an event driven graphical user interface. It consists of a loop which continuously executes, polling for user events and handling each one. After handling the event in whatever manner is appropriate, control returns to the loop until another event is received, and so on. The loop is at the highest level of control within the program, hence main .
This approach is in contrast to a number of other alternatives:
Most modern applications feature a main loop, though in contrast to earlier designs, the use of preemptive multitasking means that the loop is often stopped, and is only restarted when there is actually something ready for it to process. This is more efficient than the use of polling, which was the usual approach when cooperative multitasking was used.
This Pseudocode shows a typical application main loop:
begin( my_program ) initialize_everything; mainloop: event = next_event; process_event( event ); if ( event = quit ) terminate; else goto mainloop; end;|
|
