All Topics  
Event-driven programming

 

   Email Print
   Bookmark   Link






 

Event-driven programming



 
 
In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, event-driven programming or event-based programming is a programming paradigm
Programming paradigm

A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
 in which the flow of the program is determined by event
Event (computing)

In computing an event is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program....
s — i.e., sensor
Sensor

A sensor is a device that measures a physical quantity and converts it into a signal which can be read by an observer or by an instrument. For example, a mercury thermometer converts the measured temperature into expansion and contraction of a liquid which can be read on a calibrated glass tube....
 outputs or user actions (mouse clicks, key presses) or messages
Message passing

Message passing in computer science, is a form of communication used in parallel computing, object-oriented programming, and interprocess communication....
 from other programs or threads
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
.

Event-driven programming can also be defined as an application architecture technique in which the application has a main loop which is clearly divided down to two sections: the first is event selection (or event detection), and the second is event handling.






Discussion
Ask a question about 'Event-driven programming'
Start a new discussion about 'Event-driven programming'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, event-driven programming or event-based programming is a programming paradigm
Programming paradigm

A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
 in which the flow of the program is determined by event
Event (computing)

In computing an event is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program....
s — i.e., sensor
Sensor

A sensor is a device that measures a physical quantity and converts it into a signal which can be read by an observer or by an instrument. For example, a mercury thermometer converts the measured temperature into expansion and contraction of a liquid which can be read on a calibrated glass tube....
 outputs or user actions (mouse clicks, key presses) or messages
Message passing

Message passing in computer science, is a form of communication used in parallel computing, object-oriented programming, and interprocess communication....
 from other programs or threads
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
.

Event-driven programming can also be defined as an application architecture technique in which the application has a main loop which is clearly divided down to two sections: the first is event selection (or event detection), and the second is event handling. In embedded systems the same may be achieved using interrupt
Interrupt

In computing, an interrupt is an asynchronous communication signal from hardware indicating the need for attention or a synchronous event in software indicating the need for a change in execution....
s instead of a constantly running main loop; in that case the former portion of the architecture resides completely in hardware.

Event-driven programs can be written in any language, although the task is easier in languages that provide high-level abstraction
Abstraction (computer science)

In computer science, abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time....
s, such as closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
. Some integrated development environment
Integrated development environment

An integrated development environment also known as integrated design environment or integrated debugging environment is a software application that provides comprehensive facilities to computer programmers for software development....
s provide code generation assistants that automate the most repetitive tasks required for event handling.

Contrast with batch programming


In contrast, in batch programming, the flow is determined by the programmer. Although batch programming is the style taught in beginning programming classes, the more complex event-driven programming is the standard architecture of modern interactive programs.

Here are two pseudocode
Pseudocode

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading....
 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



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 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
Event handler

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....
.

A slight variation in the above further illustrates the point:

set counter K to 0
repeat



Example: reading from a socket

This example uses pseudocode
Pseudocode

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading....
 to illustrate how data is read from a socket
Socket

Socket can refer to:In mechanics:* Socket wrench, a type of wrench that uses separate, removable sockets to fit different sizes of nuts and bolts...
 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
Tcl

Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands....
 code to illustrate how data is read from a socket using an event-driven approach:

# open channel set chan [socket $host $port] set buffer "" fconfigure $chan -blocking none # register event handler fileevent $chan readable [list read_next_data $chan buffer]

# process event until end of file proc read_next_data

Event handlers

Because the code for checking for events and the main loop does 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

globally declare the counter K and the integer T.
OnKeyEnter(character C)



While keeping track of history is straightforward 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
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
, called event-handler routines. These routines handle the events that the main program will respond to. For example, in a GUI
Graphical user interface

A graphical user interface is a type of user interface which allows people to human-computer interaction such as computers; hand-held devices such as MP3 Players, Portable Media Players or Gaming devices; household appliances and office equipment....
 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
Database

A database is a structured collection of records or data that is stored in a computer system. The structure is achieved by organizing the data according to a database model....
 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

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.

See also

  • Signal programming
    Signal programming

    Signal programming is often used in the same sense as Event-driven programming.The word signal is used instead of the word event in documentation of such Programming library as Qt, GTK+ and libsigc++....
     (a similar concept)
  • Programming paradigm
    Programming paradigm

    A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
  • Hardware Description Language
    Hardware description language

    In electronics, a hardware description language or HDL is any language from a class of computer languages and/or programming languages for formal description of digital logic and electronic circuits....
  • SEDA
    Staged event-driven architecture

    SEDA is an acronym for "staged event-driven architecture", and refers to an approach to software design that decomposes a complex, event-driven computer program into a set of stages connected by queues....
  • Event Stream Processing
    Event Stream Processing

    Event Stream Processing, or ESP, is a set of technologies designed to assist the construction of Event-driven architecture. ESP technologies include event visualization, event databases, event-driven middleware, and event processing languages, or complex event processing ....
     (a similar concept)
  • Message-oriented middleware
    Message-oriented middleware

    Message-oriented middleware is a client/server infrastructure that increases the interoperability, portability, and flexibility of an application by allowing the application to be distributed over multiple heterogeneous platforms....
  • Publish/subscribe
    Publish/subscribe

    Publish/subscribe is an asynchronous messaging paradigm where senders of messages are not programmed to send their messages to specific receivers ....
  • Virtual synchrony
    Virtual synchrony

    Virtual synchrony is an interprocess messaging passing technology. Virtual synchrony systems allow programs running in a network to organize themselves into process groups, and to send messages to groups ....
    , a distributed execution model for event-driven programming
  • Event-driven architecture


External links

  • from Portland Pattern Repository
    Portland Pattern Repository

    The Portland Pattern Repository is a repository for design pattern s. It was accompanied by a companion website, WikiWikiWeb, which was the world's first wiki....
  • Tutorial "" by Stephen Ferg
  • Tutorial "" by Alan Gauld
  • Article "" by Martin Fowler
    Martin Fowler

    Martin Fowler is an author and international speaker on software development, specializing in object oriented programming analysis and design, Unified Modeling Language, Design pattern , and agile software development methodologies, including extreme programming....
  • Article "" by Ben Watson
    Ben Watson

    Ben Watson may refer to:*Ben Watson , , English footballer, currently playing for Wigan Athletic F.C.*Ben Watson , English footballer, currently playing for Exeter City F.C....
  • Article "" by Jonathan Simon
    Jonathan Simon

    'Jonathan Simon' is the Associate Dean of the Jurisprudence and Social Policy Program at Boalt Hall School of Law at University of California, Berkeley, author of Governing Through Crime: How the War on Crime Transformed American Democracy and Created a Culture of Fear and Poor Discipline: Parole and the Social Control of the Underclass,...
  • Article "" by Chris McDonald
  • Article "" by Christopher Diggins
  • Article "" by Stefan Schiffer and Joachim Hans Fröhlich
  • Chapter ""
  • by Tim Boudreau
  • Complex Event Processing and Service Oriented Architecture
  • Event-driven programming and SOA: Jack van Hoof
  • For an open source example, see
  • Event-driven programming in Java, see open source project [https://jsasb.dev.java.net/ Jsasb] by Rex Young
  • For C-Code generation from UML State-Charts for Embedded Systems see