Event-driven programming is a computer programming paradigm. Unlike traditional programs, which follow their own control flow pattern, only sometimes changing course at branch points, the control flow of event-driven programs is largely driven by external events.
The method by which information on events is acquired by the underlying system is immaterial. Inputs can be polled in the event loop, or interrupt handlers can be registered to react to hardware events; many systems use a mixture of both techniques. The preprogrammed algorithm ensures that triggers provided are executed when they are needed, thus providing a software abstraction that emulates an interrupt driven environment.
Event-driven programs typically consist of a number of small programs called event handlers, which are to be called in response to external events, and a dispatcher, which calls the event handlers, often using an event queue to hold unprocessed events.
In many cases event handlers can trigger events themselves, possibly leading to an event cascade.
Event-driven programming stresses flexibility and asynchrony as virtues, and tries to be as modeless as possible. Graphical user interface programs are typically programmed in an event-driven style.
Computer operating systems are another classic example of event-driven programs on at least two levels. At the lowest level, interrupt handlers act as direct event handlers for hardware events, with the CPU hardware performing the role of the dispatcher. Operating systems also typically act as dispatchers for software processes, passing data and software interrupts to user processes that in many cases are programmed as event handlers themselves.
A command line interface can be viewed as a special case of the event driven model in which the system, which is inactive, awaits one very complex event--the entry of a command by the user.
Coroutine libraries can be used to provide thread-like notation around a traditional event loop core. Ruby is a popular scripting language that takes this approach. Ruby threads are simple coroutines scheduled around an event loop. Libtask * provides a similar facility for C.
Event-driven programming is a flexible way to allow your programs to respond to many different inputs or events.
This example uses pseudocode to illustrate how data is read from a socket using an event driven approach:
function read_next_data(fd) data = read_async( fd ) if len(data) == 0 => Nothing to read, register to be called back when something is ready event_polling_register( fd, read_next_data ) => Go back to doing something else else => Data was available and len(data) was received add_data_to_buffer( buffer, data ) fi
This example uses Tcl code to illustrate how data is read from a socket using an event driven approach:
# open channel set chan $host $port set buffer "" fconfigure $chan -blocking none # register event handler fileevent $chan readable read_next_data $chan buffer # process event until end of file proc read_next_data {chan bufferVar} { upvar #0 $bufferVar buffer append buffer $chan if {$chan} {close $chan} } Event-driven programming is a flexible way to allow your programs to respond to many different inputs or events.
Ereignis (Programmierung) | Programmazione ad eventi | תכנות מונחה אירועים | Event | イベント駆動型プログラミング | Programowanie zdarzeniowe
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Event-driven programming".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world