请输入您要查询的百科知识:

 

词条 Event-driven programming
释义

  1. Event handlers

     A trivial event handler  Creating event handlers  Exception handlers in PL/I 

  2. Common uses

  3. Criticism

  4. Stackless threading

  5. See also

  6. References

  7. External links

{{multiple|{{refimprove|date=May 2012}}{{External links|section|date=March 2013}}
}}{{Programming paradigms}}

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 or 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. This is also true of programming for device drivers (e.g., P in USB device driver stacks[1]).

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 await and closures.

Event handlers

{{Main article|Event handler}}

A trivial event handler

Because the code for checking for events and the main loop are common amongst applications, 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 normally trivial in a sequential program, because event handlers execute in response to external events, correctly structuring the handlers to work correctly when called in any order can require 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 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.

Exception handlers in PL/I

In PL/I, 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 cleaning routines to clean up afterwards before termination, or to perform recovery operations and return to the interrupted procedure.

Common uses

Most of existing GUI development tools and architectures rely on event-driven programming.[2] The Java AWT framework processes all UI changes on a single thread, called the Event dispatching thread. Similarly, all UI updates in the Java framework JavaFX occur on the JavaFX Application Thread.[3]

In addition, systems such as Node.js are also event-driven.[4]

Criticism

The design of those programs which rely on event-action model has been criticised, and it has been suggested that the event-action model leads programmers to create error-prone, difficult to extend and excessively complex application code.[2] Table-driven state machines have been advocated as a viable alternative.[5] On the other hand, table-driven state machines themselves suffer from significant weaknesses including state explosion phenomena.[6]

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, which allows an extremely large number of threads to be handled. This is essentially a finite-state machine approach.

See also

  • Autonomous peripheral operation
  • Comparison of programming paradigms
  • Dataflow programming (a similar concept)
  • DOM events
  • Event-driven architecture
  • Event stream processing (a similar concept)
  • Hardware description language
  • Interrupt
  • Inversion of control
  • Message-oriented middleware
  • Programming paradigm
  • Publish–subscribe pattern
  • Reactor pattern
  • Signal programming (a similar concept)
  • Staged event-driven architecture (SEDA)
  • Time-triggered system (an alternative architecture for computer systems)
  • Virtual synchrony, a distributed execution model for event-driven programming

References

1. ^{{cite web |url=https://www.microsoft.com/en-us/research/publication/p-safe-asynchronous-event-driven-programming/ |title=P: Safe Asynchronous Event-Driven Programming |authors=Vivek Gupta, Ethan Jackson, Shaz Qadeer and Sriram Rajamani |accessdate=20 February 2017}}
2. ^{{Cite news|url=http://www.ddj.com/cpp/184401643|title=Who Moved My State?|last=Samek|first=Miro|date=April 1, 2013|work=Dr. Dobb's|access-date=2018-01-28|archive-url=|archive-date=|dead-url=}}
3. ^{{cite web |url=https://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm |title=Concurrency in JavaFX |last=Fedortsova |first=Irina |date=June 2012 |website=JavaFX Documentation Home |publisher=Oracle |access-date=4 January 2018 |quote=The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread. }}
4. ^Node.js & Event-driven programming.
5. ^{{cite web |last=Samek |first=Miro |title=State Machines for Event-Driven Systems |url=http://www.barrgroup.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems |accessdate=19 March 2013 |date=11 March 2009}}
6. ^{{cite book |title=A Practical Introduction to Hardware/Software Codesign |author=Patrick Schaumont |isbn=978-1-4614-3737-6|date=2012-11-27 }}

External links

  • Concurrency patterns presentation given at scaleconf
  • Event-Driven Programming: Introduction, Tutorial, History, tutorial by Stephen Ferg
  • Event Driven Programming, tutorial by Alan Gauld
  • Event Collaboration, article by Martin Fowler
  • 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
  • [https://archive.is/20121208205239/http://zone.ni.com/devzone/devzone.nsf/webcategories/FCE7EA7ECA51169C862567A9005878EA LabWindows/CVI Resources]
  • Distributed Publish/Subscribe Event System, an open source example which is in production on MSN.com and Microsoft.com
{{Programming language}}{{DEFAULTSORT:Event-Driven Programming}}

3 : Programming paradigms|Events (computing)|Articles with example pseudocode

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/20 10:25:31