Event-driven programming
In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by external events. Typical event can be UI events from mice, keyboards, touchpads and touchscreens, or external sensor inputs, or be programmatically generated (message passing) from other programs or threads, or network events.
Event-driven programming is the dominant paradigm used in graphical user interfaces applications and network servers.
In an event-driven application, there is generally an event loop that listens for events and then triggers a callback function when one of those events is detected.
Event-driven programs can be written in any programming language, although the task is easier in languages that provide high-level abstractions.
Although they do not exactly fit the event-driven model, interrupt handling and exception handling have many similarities.
Event loop
Because the event loop of retrieving/dispatching of events 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.
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.
Event handlers
The actual logic is contained in 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 IDEs provide the programmer with GUI event templates, allowing the programmer to focus on writing the event code.
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 when called in any order can require special attention and planning in an event-driven program.
In addition to writing the event handlers, event handlers also need to be bound to events so that the correct function is called when the event takes place. For UI events, many IDEs combine the 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.
Common uses
Most existing GUI architectures use event-driven programming.[1] Windows has an event loop. 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.[2]
Most network servers and frameworks such as Node.js are also event-driven.[3]
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.[1] Table-driven state machines have been advocated as a viable alternative.[4] On the other hand, table-driven state machines themselves suffer from significant weaknesses including the state explosion phenomenon.[5] A solution for this is to use Petri nets.
Interrupt and exception handling
See also
- Autonomous peripheral operation
- Comparison of programming paradigms
- Dataflow programming
- 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.0 1.1 Samek, Miro (April 1, 2013). "Who Moved My State?". Dr. Dobb's. http://www.ddj.com/cpp/184401643.
- ↑ Fedortsova, Irina (June 2012). "Concurrency in JavaFX". Oracle. https://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm. "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."
- ↑ Event-Driven Programming in Node.js.
- ↑ Samek, Miro (11 March 2009). "State Machines for Event-Driven Systems". http://www.barrgroup.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems.
- ↑ Patrick Schaumont (2012-11-27). A Practical Introduction to Hardware/Software Codesign. ISBN 978-1-4614-3737-6.
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
- Schiffer, S.; Frohlich, J.H. (1994). "Concepts and architecture of Vista-a multiparadigm programming environment". Proceedings of 1994 IEEE Symposium on Visual Languages. pp. 40–47. doi:10.1109/VL.1994.363642. ISBN 0-8186-6660-9.
- 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
- Javascript Event loop
Original source: https://en.wikipedia.org/wiki/Event-driven programming.
Read more |