Вы находитесь на странице: 1из 12

Event-driven programming

From Wikipedia, the free encyclopedia

This article needs additional citations for verification. Please help improve this article byadding citations to reliable sources. Unsourced material may be challenged and removed.(May 2012)

Programming paradigms

Functional Data-driven Action

Agent-oriented

Aspect-oriented

Automata-based

Component-based

Flow-based

Pipelined

Concatenative

Concurrent computing

Relativistic programming

Declarative (contrast: Imperative)

Constraint

Dataflow

Cell-oriented (spreadsheets)

Reactive

Intensional

Logic


Intentional End-user programming

Abductive logic

Answer set

Constraint logic

Functional logic

Inductive logic

Event-driven

Service-oriented

Time-driven

Expression-oriented

Feature-oriented

Function-level (contrast: Value-level)

Generic

Imperative (contrast: Declarative)

Procedural

Language-oriented

Discipline-specific

Domain-specific

Grammar-oriented

Dialecting

Metaprogramming

Automatic

Reflective

Attribute-oriented

Homoiconic

Template

Policy-based

Non-structured (contrast: Structured)

Array

Nondeterministic

Parallel computing

Process-oriented

Programming in the large and small

Semantic

Structured (contrast: Non-structured)

Modular (contrast: Monolithic)

Object-oriented

By separation of concerns:

Aspect-oriented

Role-oriented

Subject-oriented

Class-based

Prototype-based

Recursive

Value-level (contrast: Function-level)

Probabilistic

In computer programming, event-driven programming (EDP) or event-based programming is a programming paradigm in which the flow of the program is determined by eventse.g., sensor outputs or user actions (mouse clicks, key presses) or messagesfrom 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) 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 incomputer 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, such as Microsoft Visual Studio, provide code generationassistants that automate the most repetitive tasks required for event handling.
Contents
[hide]

1 Event handlers

1.1 A trivial

event handler

o
handlers

1.2 Exception

1.3 Creating event handlers 2 Criticism and best

practice 3 Stackless threading 4 See also 5 References

6 External links

[edit]Event [edit]A

handlers

trivial event handler

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 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. [edit]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 prevents further processing. Exception handlers may be provided by "ON statements" in (unseen) callers to provide housekeeping routines to clean up afterwards before termination. [edit]Creating

event handlers

The first step in developing an event-driven program is to write a series of subroutines, ormethods, 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 adatabase or exit the application. Many modern day programming environments provide the programmer with event templates so that the programmer only needs to supply 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 builtin 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. [edit]Criticism

and best practice

Event-driven programming is widely used in graphical user interfaces because it has been adopted by most commercial widget toolkitsas the model for interaction. 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,
Such an approach is fertile ground for bugs for at least three reasons:

1. 2.

It always leads to convoluted conditional logic. Each branching point requires evaluation of a complex expression.

3.

Switching between different modes requires modifying many variables, which all can easily lead to inconsistencies.

Miro Samek, Who Moved My State?, C/C++ Users Journal, The Embedded Angle column (April 2003)

and advocates the use of state machines as a viable alternative.[1] [edit]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. [edit]See

also

Interrupt Comparison of programming paradigms Dataflow programming (a similar concept) 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 (Staged Event-Driven Architecture) Virtual synchrony, a distributed execution model for event-driven programming

[edit]References

1.

^ Samek, Miro (11 March 2009). "State Machines for Event-Driven Systems". Retrieved 19 March 2013.

[edit]External

links

This section's use of external links may not follow Wikipedia's policies or guidelines.Please improve this article by removing excessive or inappropriate external links, and converting useful links where appropriate into footnote references. (March 2013)

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 Frhlich

Event-Driven Programming and Agents, chapter

LabWindows/CVI Resources Complex Event Processing and Service Oriented Architecture How EDA extends SOA and why it is important by Jack van Hoof 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

[hide]

Types of programming languages

Array

Aspect-oriented

Assembly

Class-based

Compiled

Concatenative

Concurrent

Data-structured

Dataflow

Declarative

Domain-specific Dynamic

Esoteric

Event-driven

Extensible

Functional

High-level

Imperative

Interpreted

Logic

Low-level

Machine

Macro

Metaprogramming Multi-paradigm

Non-English-based

Object-based

Object-oriented

Off-side rule

Pipeline

Procedural

Prototype-based

Reflective

Rule-based Scripting

Synchronous

Very high-level

Visual

Categories:

Programming paradigms Events (computing)

Navigation menu

Create account Log in

Article Talk Read Edit View history


Main page Contents

Featured content Current events Random article Donate to Wikipedia

Interaction Help


Toolbox

About Wikipedia Community portal Recent changes Contact Wikipedia

Print/export Languages Deutsch

Espaol Franais Galego Hrvatski Italiano

Nederlands Polski Portugus Romn Simple English Trke


This page was last modified on 19 March 2013 at 07:30.

Edit links

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of Use for details. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.

Contact us