Revision as of 12:58, 8 October 2009 editClueBot (talk | contribs)1,596,818 editsm Reverting possible vandalism by Marjaei to version by Kstevenham. False positive? Report it. Thanks, ClueBot. (785291) (Bot)← Previous edit | Revision as of 05:19, 13 October 2009 edit undo148.87.1.170 (talk) jsasb project has been discontinued and removed from host siteNext edit → | ||
Line 145: | Line 145: | ||
* Event-driven programming and SOA: Jack van Hoof | * Event-driven programming and SOA: Jack van Hoof | ||
* For an open source example which is in production on MSN.com and Microsoft.com, see | * For an open source example which is in production on MSN.com and Microsoft.com, see | ||
* Event-driven programming in Java, see open source project by Rex Young | |||
* For C-Code generation from UML State-Charts for Embedded Systems see | * For C-Code generation from UML State-Charts for Embedded Systems see | ||
* - A ClassWizard-like event-driven state machine framework and tool running in popular IDEs under open-source license. | * - A ClassWizard-like event-driven state machine framework and tool running in popular IDEs under open-source license. |
Revision as of 05:19, 13 October 2009
In computer programming, event-driven programming or event-based programming is a programming paradigm in which the flow of the program is determined by events—i.e., sensor outputs or user actions (mouse clicks, key presses) or messages from other programs or threads.
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 interrupts 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 abstractions, such as closures. Some integrated development environments 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 versions of a trivial program to add two numbers:
Batch version
read a number (from the keyboard) and store it in variable A read a number (from the keyboard) and store it in variable A print A+A
Event-driven version
set counter K to 0 repeat { if a number has been entered (from the keyboard) { store in A and increment K if K equals 2 print A+A 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 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.
A slight variation in the above further illustrates the point:
set counter K to 0 repeat { whenever a number has been entered (from the keyboard) // keyboard-number event { store in A and increment K // keyboard-number handler } if K equals 2 // ready-to-sum event { print A+A and reset K to 0 // ready-to-sum handler } }
Example: reading from a socket
This article duplicates the scope of other articles. Please discuss this issue and help introduce a summary style to the article. |
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
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) { 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.
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 loopThe 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
- DOM events
- Event-driven architecture
- Event Stream Processing (a similar concept)
- Hardware Description Language
- Inversion of control
- Message-oriented middleware
- Programming paradigm
- Publish/subscribe
- Signal programming (a similar concept)
- SEDA
- Virtual synchrony, a distributed execution model for event-driven programming
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 UML Statecharts in C/C++, Second Edition: Event-Driven Programming for Embedded Systems, Newnes 2008, ISBN 978-0-7506-8706-5.
- Faison, Ted (2006). Event-Based Programming: Taking Events to the Limit. Apress. ISBN 1-59059-643-9.
External links
- Description from Portland Pattern Repository
- Tutorial "Event-Driven Programming: Introduction, Tutorial, History" by Stephen Ferg
- Tutorial "Event Driven Programming" by Alan Gauld
- Article "Event Collaboration" by Martin Fowler
- Article "Transitioning from Structured to Event-Driven Programming" by Ben Watson
- Article "Rethinking Swing Threading" by Jonathan Simon
- Article "The event driven programming style" by Chris McDonald
- Article "Event Driven Programming using Template Specialization" by Christopher Diggins
- Article "Concepts and Architecture of Vista - a Multiparadigm Programming Environment" by Stefan Schiffer and Joachim Hans Fröhlich
- Chapter "Event-Driven Programming and Agents"
- LabWindows/CVI Resources
- Comment by Tim Boudreau
- Complex Event Processing and Service Oriented Architecture
- Event-driven programming and SOA: How EDA extends SOA and why it is important Jack van Hoof
- For an open source example which is in production on MSN.com and Microsoft.com, see Distributed Publish/Subscribe Event System
- For C-Code generation from UML State-Charts for Embedded Systems see Sinelabore.com
- StateWizard - A ClassWizard-like event-driven state machine framework and tool running in popular IDEs under open-source license.