✔ 最佳答案
In computing, the main loop (sometimes called the program loop, 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:
Traditionally, a program simply ran once then terminated. This type of program was very common in the early days of computing, and lacked any form of user interactivity. This is still used a lot, particularly in the form of command line driven programs. Any parameters are set up in advance and passed in one go when the program starts.
Menu-driven designs. These still may feature a main loop but are not usually thought of as event driven in the usual sense. Instead, the user is presented with an ever-narrowing set of options until the task they wish to carry out is the only option available. Limited interactivity through the menus is available.
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;