Information about Event Driven Programming

Event-driven programming or event-based programming is a computer programming paradigm in which the flow of the program is determined by user actions (mouse clicks, key presses) or messages from other programs. In contrast, in batch programming or flow-driven programming the flow is determined by the programmer. Batch programming is the style taught in beginning programming classes while event-driven programming is what is needed in any interactive program. Event-driven programs can be written in any language, although the task is easier in some languages than in others. Some programming environments make the task quite easy, others less so.

Overview

Instead of waiting for a complete command to make it process information, the system is preprogrammed with an event loop, to look repeatedly for information to process (whether this might be the appearance of a file in a folder, a keyboard or mouse operation, or a timer event) and then perform a trigger function to process it. Programming an event-driven system is thus a matter of rewriting the default trigger functions of the system, to match the required behavior.

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 [1] 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.

Contrast with batch programming

Here are two pseudocode versions of a trivial program to add two numbers:

Batch version

>
read a number (from the keyboard) and store it in variable A[0]
read a number (from the keyboard) and store it in variable A[1]
print A[0]+A[1]

Event-driven version

>
set counter K to 0
repeat {
   if a number has been entered (from the keyboard) {
       store in A[K] and increment K
       if K equals 2 print A[0]+A[1] and reset K to 0
   }
}


At first sight, the event-driven program seems more cumbersome and for such a trivial task is indeed so. However, the second program can be made generalized far more easily than the first. Instead of checking just for a number entry we may add code to check whether any of several events has occurred. Then for each event we can execute a particular piece of code that is commonly referred to as an event handler.

Example: reading from a socket

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 ) end_if end_function

This example uses Tcl code to illustrate how data is read from a socket using an event-driven approach:
  1. open channel
set chan [socket $host $port] set buffer "" fconfigure $chan -blocking none
  1. register event handler
fileevent $chan readable [list read_next_data $chan buffer]
  1. process event until end of file
proc read_next_data {chan bufferVar} { upvar #0 $bufferVar buffer append buffer [read $chan] if {[eof $chan]} {close $chan} }

Event handlers

Because the code for checking for events and the main loop do not depend on the application, many programming frameworks take care of their implementation and expect the user to provide only the code for the event handlers. In this simple example there may be a call to event handler called OnKeyEnter() that includes an argument with a string of characters, corresponding to what the user typed before hitting the ENTER key. If we want to add two numbers we need to use storage outside the event handler, so the implementation might look like this

A trivial event handler

>
declare globally the counter K and the integer T.
OnKeyEnter(character string S)
{
   convert S to a number N
   if K is zero store N in T and increment K
   otherwise add N to T, print the result and reset K to zero
}
Of course, using global variables is not a good idea and a better solution is to use object oriented programming making the event handler a method of an object that also holds the information necessary between calls to the event handler.

While keeping track of history is straightfoward in a batch program, it requires special attention and planning in an event-driven program.

Creating event handlers

The first step in developing an event-driven program is to write a series of subroutines, or methods, called event-handler routines. These routines handle the events that the main program will respond to. For example, in a GUI program, we might be interested in a single (as opposed to a double) left-button mouse-click on a command button. So a routine would be written to respond to such an event. The routine might open another window, save data to a database or exit the application. Many modern day programming environments provide the programmer with event templates so that the programmer need only supply the event code.

Binding event handlers

The second step is to bind event handlers to events, so that the correct function is called when the event takes place.

Graphical editors combine the first two steps: double-click on a button, and the editor creates an (empty) event handler associated with the user clicking the button and opens a text window so you can edit the event handler.

Main loop

Main article: main loop
The third step in developing an event-driven program is to write the main loop: a function that checks for events, and then calls the matching event handler. Most event-driven programming environments already provide this main loop, so it need not be rewritten.

Frameworks and libraries

  • Eiffel Event Library http://se.ethz.ch/download
  • Liboop, event loop management library http://liboop.ofb.net/
  • libsigc++, a callback framework for C++
  • libevent, an event notification library for C/C++
  • REBECA, Event-Based Electronic Commerce Architecture
  • Tcl, Tcl language has event-driven programming built in
  • Twisted, Python
  • POE, Perl
  • PRADO, a component-based and event-driven Web programming framework for PHP 5
  • Gui4Cli, an event-driven programming language for Windows
  • The Qt Toolkit, a cross-platform GUI toolkit for C++ based on an event-driven model. A version called Qt/Console exists which omits the GUI features, but still includes the event-handling framework and some other features like cross-platform networking, threading, and XML libraries.
  • GLib

See also

References

  • Grant Palmer: Java Event Handling, Prentice Hall, ISBN 0-13-041802-1.
  • David Luckham: The Power of Events - An Introduction to Complex Event Processing in Distributed Enterprise Systems, Addison-Wesley, ISBN 0-201-72789-7.
  • George S. Fishman: Discrete-Event Simulation - Modeling, Programming, and Analysis, Springer, ISBN 0-387-95160-1.
  • Bertrand Meyer (2004): The power of abstraction, reuse and simplicity: an object-oriented library for event-driven design, in Festschrift in Honor of Ole-Johan Dahl, eds. Olaf Owe et al., Springer-Verlag, Lecture Notes in Computer Science 2635, also available online.
  • Miro Samek: Practical Statecharts in C/C++: Quantum Programming for Embedded Systems, CMP Books, ISBN 1-57820-110-1.
  • Faison, Ted (2006). . Apress. ISBN 1-59059-643-9. 

External links

Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. The source code is written in a programming language.
..... Click the link for more information.
Since the late 1960s, the word paradigm (IPA: /ˈpærədaɪm/) has referred to a thought pattern in any scientific discipline or other epistemological context.
..... Click the link for more information.
In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions or function calls of an imperative or functional program are executed or
..... Click the link for more information.
Editing of this page by unregistered or newly registered users is currently disabled due to vandalism.
If you are prevented from editing this page, and you wish to make a change, please discuss changes on the talk page, request unprotection, log in, or .
..... Click the link for more information.
In computer science, message passing is a form of communication used in concurrent computing, parallel computing, object-oriented programming, and interprocess communication. Communication is made by the sending of messages to recipients.
..... Click the link for more information.
Batch processing is execution of a series of programs ("jobs") on a computer without human interaction.

Batch jobs are set up so they can be run to completion without human interaction, so all input data is preselected through scripts or commandline parameters.
..... Click the link for more information.
Flow-driven programming is a computer programming paradigm used by traditional programs, which follow their own control flow pattern, only sometimes changing course at branch points (in this, it is markedly similar to procedural programming).
..... Click the link for more information.
integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, a compiler and/or interpreter, build automation tools, and (usually)
..... Click the link for more information.
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.
..... Click the link for more information.
Polling, or polled operation, in computer science, refers to actively sampling the status of an external device by a client program as a synchronous activity. Polling is most often used in terms of I/O, and is also referred to as polled I/O.
..... Click the link for more information.
An interrupt handler, also known as an interrupt service routine (ISR), is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt.
..... Click the link for more information.
In computer programming, an event handler is an asynchronous callback subroutine that handles inputs received in a program. Each event is a piece of application-level information from the underlying framework, typically the GUI toolkit.
..... Click the link for more information.
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. This is done to support the cases where the appropriate method cannot be determined at compile-time (i.e. statically).
..... Click the link for more information.
In computer science, a message queue is a software-engineering component used for interprocess communication or inter-thread communication within the same process. It uses a queue for messaging – the passing of control or of content.
..... Click the link for more information.
In event-driven computer programs, an event cascade occurs when an event handler causes another event to occur which also triggers an event handler. This can be a tricky source of program errors (see computer bug).
..... Click the link for more information.
Asynchrony, in the general meaning, is the state of not being synchronized.
  • Asynchronous learning
  • Collaborative editing systems
In specific terms of digital logic and physical layer of communication, an asynchronous process does not require a clock signal.
..... Click the link for more information.
Modelessness is a property of computer user interfaces. In a modeless interface, the same input from the user will always trigger the same perceived action. This is opposed to a modal interface, where the action perceived to have been executed is dependent on the current state of
..... Click the link for more information.
graphical user interface (GUI) is a type of user interface which allows people to interact with a computer and computer-controlled devices which employ graphical icons, visual indicators or special graphical elements called "widgets", along with text, labels or text
..... Click the link for more information.
An operating system (OS) is the software that manages the sharing of the resources of a computer. An operating system processes system data and user input, and responds by allocating and managing tasks and internal system resources as a service to users and programs of the
..... Click the link for more information.
An interrupt handler, also known as an interrupt service routine (ISR), is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt.
..... Click the link for more information.
central processing unit (CPU), or sometimes simply processor, is the component in a digital computer capable of executing a program.(Knott 1974) It interprets computer program instructions and processes data.
..... Click the link for more information.
In computing, an interrupt is an asynchronous signal from hardware indicating the need for attention or a synchronous event in software indicating the need for a change in execution.
..... Click the link for more information.
command line interface or CLI is a method of interacting with an operating system or software using a command line interpreter. This command line interpreter may be a text terminal, terminal emulator, or remote shell client such as PuTTY.
..... Click the link for more information.
In computer science, coroutines are program components that generalize subroutines to allow multiple entry points and suspending and resuming of execution at certain locations.
..... Click the link for more information.
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.
..... Click the link for more information.
Ruby

Paradigm: multi-paradigm (functional, imperative, logic, object-oriented (class-based))
Appeared in: 1995
Designed by: Yukihiro Matsumoto
Developer: Yukihiro Matsumoto (among others)
Latest release: 1.8.
..... Click the link for more information.
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.
..... Click the link for more information.
C

The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language.
..... Click the link for more information.
Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax.
..... Click the link for more information.
In computer programming, an event handler is an asynchronous callback subroutine that handles inputs received in a program. Each event is a piece of application-level information from the underlying framework, typically the GUI toolkit.
..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter