| advertise add site services publishers database health videos | ![]() | about toolbar stats live show health store more stuff JOIN/LOGIN |
Loop de Loop Results 2003 empirerunners.org | Teaching for Medispa Program, Laser Programs, Beauty Services, Permanent... medispa-resources.com | SURG-I-LOOP Silicone Loops | Scanlan International scanlaninternational.com |
In computer science, the event loop, message dispatcher, message loop or message pump is a programming construct that waits for and dispatches events or messages in a program. It works by polling some internal or external "event provider", which generally blocks until an event has arrived, and then calls the relevant event handler ("dispatches the event"). The event-loop may be used in conjunction with a reactor, if the event provider follows the file interface (which can be select()ed or poll()ed). The event loop almost always operates asynchronously with the message originator. When the event loop forms the central control flow construct of a program, as it often does, and is thus at the highest level of control within the program, it may be termed the main loop or main event loop.
[edit] Message passingMessage pumps are said to 'pump' messages from the program's message queue (assigned and usually owned by the underlying operating system) into the program for processing. In the strictest sense, an event loop is one of the methods for implementing inter-process communication. In fact, message processing exists in many systems, including a kernel-level component of the Mach operating system. The event loop is a specific implementation technique of systems that use message passing. [edit] Alternative designsThis approach is in contrast to a number of other alternatives:
[edit] UsageDue to the predominance of GUI interfaces, most modern applications feature a main loop. The get_next_message() routine is typically provided by the operating system, and blocks until a message is available. Thus, the loop is only entered when there is something to process. function main initialize() while program_running message := get_next_message() if message = quit then return end if process_message(message) repeat end function [edit] File interfaceUnder Unix, the "everything is a file" paradigm naturally leads to a file-based event loop. Reading from and writing to files, inter-process communication, network communication and device control are all achieved using file I/O, with the target identified by a file descriptor. The select and poll system calls allow a set of file descriptors to be monitored for a change of state, e.g. when data becomes available to be read. For example, consider a program that reads from a continuously updated file and displays its contents in the X Window System, which communicates with clients over a socket (either Unix domain or Berkeley): main(): file_fd = open ("logfile") x_fd = open_display () construct_interface () while changed_fds = select ({file_fd, x_fd}): if file_fd in changed_fds: data = read_from (file_fd) append_to_display (data) send_repaint_message () if x_fd in changed_fds: process_x_messages () [edit] Handling signalsOne of the few things in Unix that do not conform to the file interface are asynchronous events (signals). Signals are received in signal handlers, small, limited pieces of code that run while the rest of the task is suspended; if a signal is received and handled while the task is blocking in Thus an obvious way to handle signals is for signal handlers to set a global flag and have the event loop check for the flag immediately before and after the The solution arrived at by POSIX is the pselect call, which is similar to An alternative, more portable solution, is to convert asynchronous events to file-based events using the self-pipe trick, where "a signal handler writes a byte to a pipe whose other end is monitored by select() in the main program".[1] [edit] Implementations[edit] Windows applicationsMain article: Message loop in Microsoft Windows The Microsoft Windows operating system requires user-interactive processes that wish to run on the operating system to construct a message loop for responding to events. In this operating system, a message is equated to an event created and imposed upon the operating system. An event can range from user interaction, network traffic, system processing, timer activity, and interprocess communication among others. The "heart" of most Win32 applications is the WinMain function, which calls GetMessage(), in a loop. GetMessage blocks until a message, or "event", is received. After some optional processing, it will call DispatchMessage(), which dispatches the message to the relevant handler, also known as WindowProc. Normally, messages that have no special WindowProc are dispatched to DefWindowProc, the default one. DispatchMessage calls the window-proc of the HWND handle of the message (Registered with the RegisterClass function). [edit] Message orderingMore recent versions of Microsoft Windows provide the guarantee to the programmer that messages will be delivered to an application's message loop in the order that they were perceived by the system and its peripherals. This guarantee is essential when considering the design consequences of multithreaded applications. However, some messages have different rules, such as messages that are always received last, or messages with a different documented priority.[2] [edit] X Window System[edit] Xlib event loopX applications using Xlib directly are built around the Very few programs use Xlib directly. In the more common case, GUI toolkits based on Xlib usually support adding events. For example, toolkits based on Xt intrinsics have [edit] GLib event loopThe GLib event loop was originally created for use in GTK+ but is now used in non-GUI applications as well, such as D-Bus. The resource polled is the collection of file descriptors the application is interested in; the polling block will be interrupted if a signal arrives or a timeout expires (e.g. if the application has specified a timeout or idle task). While GLib has built-in support for file descriptor and child termination events, it is possible to add an event source for any event that can be handled in a prepare-check-dispatch model.[1] Application libraries that are built on the GLib event loop include GStreamer and the asynchronous I/O methods of GnomeVFS, but GTK+ remains the most visible client library. Events from the windowing system (in X, read off the X socket) are translated by GDK into GTK+ events and emitted as GLib signals on the application's widget objects. [edit] Core Foundation run loops[edit] Report Program Generator (RPG) languageIBM RPG was designed to operate with a main loop or program cycle consisting of
and was only later adapted to use more conventional programmer directed loop handling. [edit] References
[edit] See also
[edit] External links |
| ↑ top of page ↑ | about thumbshots |