Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Event-driven programming
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Computer programming paradigm}} In [[computer programming]], '''event-driven programming''' is a [[programming paradigm]] in which the [[Control flow|flow of the program]] is determined by external [[Event (computing)|events]]. [[User interface|UI]] events from [[computer mouse|mice]], [[computer keyboard|keyboards]], [[touchpads]] and [[touchscreens]], and external [[sensor]] inputs are common cases. Events may also be programmatically generated, such as from [[message passing|messages from other programs]], notifications from other [[thread (computer science)|threads]], or other [[Computer network|network]] events. Event-driven programming is the dominant paradigm used in [[graphical user interface]]s applications and network servers. In an event-driven application, there is generally an [[event loop]] that listens for events and then triggers a [[callback (computer programming)|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 [[abstraction (computer science)|high-level abstractions]]. Although they do not exactly fit the event-driven model, [[Interrupt handler|interrupt handling]] and [[exception handling]] have many similarities. It is important to differentiate between [[Event-driven messaging|event-driven]] and [[Message queue|message-driven (aka queue driven)]] paradigms: Event-driven services (e.g. [[Amazon Simple Notification Service|AWS SNS]]) are decoupled from their consumers. Whereas queue / message driven services (e.g. [[Amazon Simple Queue Service|AWS SQS]]) are coupled with their consumers.<ref>{{Cite book |title=Domain-Driven Design with Java - A Practitioner's Guide |year=2022 |isbn=9781800564763 |last1=Chandrasekaran |first1=Premanand |last2=Krishnan |first2=Karthik |last3=Ford |first3=Neal |last4=Byars |first4=Brandon |last5=Buijze |first5=Allard |publisher=Packt Publishing }}</ref> ==Event loop== {{Main article|Event loop}}Because the [[main loop|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. [[IBM RPG|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' ([[flag (computing)|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 [[graphical user interface|GUI]] program may trigger a routine that will open another window, save data to a [[database]] or exit the application. Many [[Integrated development environment|IDEs]] provide the programmer with GUI event templates, allowing the programmer to focus on writing the event code. 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.<ref name="Samek">{{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}}</ref> Windows has an [[Message loop in Microsoft Windows|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.<ref>{{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. }}</ref> Most network servers and frameworks such as Node.js are also event-driven.<ref>[https://www.digitalocean.com/community/tutorials/nodejs-event-driven-programming/ Event-Driven Programming in Node.js].</ref> ==Interrupt and exception handling== {{Empty section|date=May 2024}} ==See also== * [[Autonomous peripheral operation]] * [[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== {{Reflist}} ==External links== *[http://shairosenfeld.com/concurrency.html Concurrency patterns presentation] given at [http://scaleconf.org scaleconf] *[http://eventdrivenpgm.sourceforge.net/ Event-Driven Programming: Introduction, Tutorial, History], tutorial by Stephen Ferg *[http://www.alan-g.me.uk/l2p/tutevent.htm Event-Driven Programming], tutorial by Alan Gauld *[http://www.martinfowler.com/eaaDev/EventCollaboration.html Event Collaboration], article by Martin Fowler *[https://web.archive.org/web/20150919021358/https://today.java.net/pub/a/today/2003/10/24/swing.html Rethinking Swing Threading], article by Jonathan Simon *[http://www.csse.uwa.edu.au/cnet/eventdriven.html The event-driven programming style] {{Webarchive|url=https://web.archive.org/web/20050220142724/http://www.csse.uwa.edu.au/cnet/eventdriven.html |date=2005-02-20 }}, article by Chris McDonald *[https://web.archive.org/web/20050207025012/http://www.codeproject.com/cpp/static-callbacks.asp Event Driven Programming using Template Specialization], article by Christopher Diggins *{{Cite book|doi=10.1109/VL.1994.363642|isbn=0-8186-6660-9|chapter=Concepts and architecture of Vista-a multiparadigm programming environment|title=Proceedings of 1994 IEEE Symposium on Visual Languages|year=1994|last1=Schiffer|first1=S.|last2=Frohlich|first2=J.H.|pages=40–47|s2cid=15927869}} *[http://docs.eiffel.com/book/method/8-event-driven-programming-and-agents Event-Driven Programming and Agents], chapter *[https://archive.today/20121208205239/http://zone.ni.com/devzone/devzone.nsf/webcategories/FCE7EA7ECA51169C862567A9005878EA LabWindows/CVI Resources] *[http://www.codeplex.com/pubsub Distributed Publish/Subscribe Event System], an open-source example which is in production on MSN.com and Microsoft.com {{Programming paradigms navbox}} {{Types of programming languages}} {{DEFAULTSORT:Event-Driven Programming}} [[Category:Programming paradigms]] [[Category:Events (computing)]] [[Category:Articles with example pseudocode]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite news
(
edit
)
Template:Cite web
(
edit
)
Template:Empty section
(
edit
)
Template:Main article
(
edit
)
Template:Programming paradigms navbox
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Types of programming languages
(
edit
)
Template:Webarchive
(
edit
)