World Library  
Flag as Inappropriate
Email this Article

Event-driven programming

Article Id: WHEBN0000062599
Reproduction Date:

Title: Event-driven programming  
Author: World Heritage Encyclopedia
Language: English
Subject: Comparison of programming paradigms, Object-oriented programming, Event loop, Visual Basic, UML state machine
Collection:
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Event-driven programming

In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. Event-driven programming is the dominant paradigm used in graphical user interfaces and other applications (e.g. JavaScript web applications) that are centered on performing certain actions in response to user input.

In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected. In embedded systems the same may be achieved using hardware interrupts instead of a constantly running main loop. Event-driven programs can be written in any programming language, although the task is easier in languages that provide high-level abstractions, such as closures.

Event handlers

A trivial event handler

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 an event handler called OnKeyEnter() that includes an argument with a string of characters, corresponding to what the user typed before hitting the ENTER key. To add two numbers, storage outside the event handler must be used. The implementation might look like below.

globally declare the counter K and the integer T. OnKeyEnter(character C) { convert C 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 }

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

Exception handlers

In PL/1, even though a program itself may not be predominantly event driven, certain abnormal events such as a hardware error, overflow or "program checks" may occur that possibly prevent further processing. Exception handlers may be provided by "ON statements" in (unseen) callers to provide housekeeping routines to clean up afterwards before termination.

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 to which the main program will respond. For example, a single left-button mouse-click on a command button in a GUI program may trigger a routine that will open another window, save data to a database or exit the application. Many modern day programming environments provide the programmer with event templates, allowing the programmer to focus on writing the event code.

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.

The third step in developing an event-driven program is to write the main loop. This is a function that checks for the occurrence of events, and then calls the matching event handler to process it. Most event-driven programming environments already provide this main loop, so it need not be specifically provided by the application programmer. RPG, an early programming language from IBM, whose 1960s design concept was similar to event driven programming discussed above, provided a built-in main I/O loop (known as the "program cycle") where the calculations responded in accordance to 'indicators' (flags) that were set earlier in the cycle.

Criticism and best practice

Event-driven programming is widely used in graphical user interfaces, for instance the Android concurrency frameworks are designed using the Half-Sync/Half-Async pattern,[1] where a combination of a single-threaded event loop processing (for the main UI thread) and synchronous threading (for background threads) is used. This is because the UI-widgets are not thread-safe, and while they are extensible, there is no way to guarantee that all the implementations are thread-safe, thus single-thread model alleviates this issue.

The design of those toolkits has been criticized, e.g., by Miro Samek, for promoting an over-simplified model of event-action, leading programmers to create error prone, difficult to extend and excessively complex application code. He writes,

and advocates the use of state machines as a viable alternative.[2]

Stackless threading

An event driven approach is used in hardware description languages. A thread context only needs a cpu stack while actively processing an event, once done the cpu can move on to process other event-driven threads, that allows an extremely large number of threads to be handled. This is essentially a Finite-state machine approach.

See also

References

  1. ^ Schmidt, Douglas. "Half-Sync/Half-Async - An Architectural Pattern for Efficient and Well-structured Concurrent I/O". 
  2. ^ Samek, Miro (11 March 2009). "State Machines for Event-Driven Systems". Retrieved 19 March 2013. 

External links

  • Concurrency patterns presentation given at scaleconf
  • Description from Portland Pattern Repository
  • Event-Driven Programming: Introduction, Tutorial, History, tutorial by Stephen Ferg
  • Event Driven Programming, tutorial by Alan Gauld
  • Event Collaboration, article by Martin Fowler
  • Transitioning from Structured to Event-Driven Programming, article by Ben Watson
  • Rethinking Swing Threading, article by Jonathan Simon
  • The event driven programming style, article by Chris McDonald
  • Event Driven Programming using Template Specialization, article by Christopher Diggins
  • Concepts and Architecture of Vista - a Multiparadigm Programming Environment, article by Stefan Schiffer and Joachim Hans Fröhlich
  • Event-Driven Programming and Agents, chapter
  • LabWindows/CVI Resources
  • Distributed Publish/Subscribe Event System, an open source example which is in production on MSN.com and Microsoft.com
  • Sinelabore.com, for C-Code generation from UML State-Charts for Embedded Systems
  • StateWizard, a ClassWizard-like event-driven state machine framework and tool running in popular IDEs under open-source license
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.