140

Unlike the polling example you provided (where the button is checked every frame), an event listener does not check if the button is pushed at all. Instead, it gets called when the button is pushed. Perhaps the term "event listener" is throwing you. This term suggests that the "listener" is actively doing something to listen, when in fact, it's not doing ...


106

Nope. They're really handy for implementing Observers and making sure that classes are closed to modification. Let's say we have a method that registers new users. public void Register(user) { db.Save(user); } Then someone decides that an email should be sent. We could do this: public void Register(user) { db.Save(user); emailClient.Send(...


75

An event is a notification describing an occurrence from the recent past. A typical implementation of a event-driven system utilises an event dispatcher and handler functions (or subscribers). The dispatcher provides an API to wire handlers up to events (jQuery's bind), and a method to publish an event to its subscribers (trigger in jQuery). When you're ...


54

Most event loops will suspend if there are no events ready, which means the operating system will not give the task any execution time until an event happens. Say the event is a key being pressed. You might ask if there's a loop somewhere in the operating system checking for keypresses. The answer is no. Keys being pressed generate an interrupt, which is ...


53

Nope. A classic example of events being used in non-GUI logic are database triggers. Triggers are code that gets executed when a given event happen (INSERT,DELETE, etc). Seems like an event to me. This is the Wikipedia definition of event: In computing, an event is an action or occurrence recognized by software that may be handled by the software. ...


52

An event listener akin to an e-mail newsletter subscription (you register yourself to receive updates, whose transmission is later initiated by the sender), rather than endlessly refreshing a web page (where you are the one initiating the transfer of information). An event system is implemented using event objects, which manage a list of subscribers. ...


38

The short, unsatisfactory answer is that the application receives a signal (the event) and that the routine is only called at that point. The longer explanation is a bit more involved. Where do client events come from? Each modern application† has an inner, usually semi-hidden "event loop" that dispatches events to the correct components that ...


32

There are many ways to do this, but I prefer to keep a message-based system as decoupled as possible. This means the overall system cannot read the state of any component, nor any component read the state of any other (as that way lies spaghetti ties of dependancies). So, while the running system will look after itself, we need a way to tell each component ...


31

Follow the KISS principle: Keep It Simple, Stupid, or the YAGNI principle: You Ain't Going to Need It. You can write the code like: void updateSpecialData() { // do the update. backupData(); } Or you can write code like: void updateSpecialData() { // do the update. emit SpecialDataUpdated(); } void SpecialDataUpdatedHandler() { ...


28

Event-based programming is actually also used for highly performant server programming. At a typical server workload, much of the time processing a result actually comes from I/O. For example, pulling data off a (7200 RPM) hard disk drive can take up to 8.3 ms. For a modern GHz processor, that would equate to ~1 million clock cycles. If a CPU were ...


21

This is apples and oranges: An Event Driven system can react to events passed as messages ( messages in this context are implied immutable non-shared data ) as the events are raised. This is a purely architectural design. A Message Passing system can be driven by events that create and pass the messages. This is a purely implementation design. The two are ...


19

Yep, you are definitely missing something. Gotos would typically be used, like you said, to perform a one-way transfer of control. However, events do not do that. When the code fires the event, it knows full well that once event is published (or processed, queued, fired... etc) code execution will resume on the very next line in the code that generated ...


19

That depends upon whether it is really an Event or State Notification (which is what ready is). If it is an event, then you don't tell the listener about all prior events. If it is State Notification, then you tell them immediately after they have subscribed and you are in the specified state. The tricky bit will be where it can be either a state or an ...


19

Terminology event: A type of thing that can happen. event firing: A specific occurrence of an event; an event happening. event listener: Something that looks out for event firings. event handler: Something that occurs when an event listener detects an event firing. event subscriber: A response that the event handler's supposed to call. These ...


18

In an Event-based application the concept of Event Listeners will give you the ability to write even more Loosely Coupled applications. For example a third-party module or plug-in can delete a record from the database and then trigger the receordDeleted event and leave the rest to the event listeners to do their job. Everything will work fine, even though ...


17

Routed events According to MSDN, A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event. The definition is followed by an example of a WPF node with three buttons. The single routed event is listening for three buttons. If you try to write the same code ...


17

In my experience the only specific difference is that in most message passing systems, the sender of the message is aware of (and often declares) who the recipient of the message is. So instead of raising an event and anyone who is sucscribed to the event getting it, the sender defines some id of the intended recipient(s) or logical group of recipients and ...


17

The main difference between a hook and event is loose coupling versus tight coupling. A hook is a generic way to broadcast that something has happened. You can add new hooks without having to recompile plugins, and all hooks follow a generic design pattern. Once the hook API is defined it doesn't change so the coupling between the app and plugin isn't ...


14

Event driven architecture simply means, that the main loop AKA event loop does not understand much about the data a program processes. It understands only enough, that it can dispatch the right events for the data. So for example, if event is triggered by data coming from a socket, all the event loop needs to know is, what callback to call, when data comes ...


13

I think of an event listener not as a function running its own loop, but as a relay race with the first runner waiting for the starting gun. A significant reason for using events instead of polling is that they are more efficient with CPU cycles. Why? Look at it from the hardware up (rather than the source code down). Consider a Web server. When your ...


13

The example you describe of a simple data, where the modification triggers some effect can perfectly be implemented with the observer design pattern: this is simpler to implement and maintain than full event driven code. the coupling between subject and observer can be abstract, which facilitates separation of concerns. it's ideal for one to many ...


13

In principle, a command describes a request that is to be executed, whereas an event describes something that has happened: A command requires some action to be performed by a processor, and this action should be performed only once by this processor. An event is the notification of some action that was already executed or an external happening. Several ...


13

proto3 makes a number of changes aimed (as I understand it) at making it far more usable in cross-platform scenarios. Explicit tracking of "assigned" vs "not assigned but reporting the default value" can be very hard to implement on some of the target platforms, and can also be confusing to use. As such, proto3 adopts a much simpler approach: the implicit ...


12

How do I deal with side effects in Event Sourcing? Short version: the domain model doesn't perform side effects. It tracks them. Side effects are performed using a port that connects to the boundary; when the email is sent, you send the acknowledgement back to the domain model. This means that the email is sent outside of the transaction that updates the ...


11

Much of the confusion between "message passing" and "event based" has to do with architectural vs. implementation details. I have seen (and written) event driven systems that actually use OS provided messages for their implementation. I am guessing you are really referring to architectural ideas. As many people have already pointed out "message passing" ...


11

Is event chaining a good idea? It's one of those things that seems like a really good idea, until you use it. It is very difficult to set up cascading events without some sort of implied dependency on order. It's difficult to set them up without causing issues due to infinite loops and occasional memory leaks. They make class design more difficult due to ...


10

Events are also heavily used in network programming (e.g. Nginx) to avoid expensive busy-wait loops and instead provide a clean interface to know exactly when a certain operation is available(I/O, urgent data etc). This is also a solution to the C10k problem. The basic idea is to provide the OS a set of sockets (i.e. network connections) to monitor for ...


9

One way to avoid refreshing too often would be to use a dirty_flag + a Timer; you set the Timer interval to something like 200ms or whatever value you consider optimal. When en event that requires a refresh happens, you just set the dirty_flag to true, and reinitialize the Timer(that way you make sure that Timer.Tick will be raised at "x" milliseconds after ...


8

No. It is not "optimized polling." An event-loop uses interrupt-driven I/O instead of polling. While, Until, For, etc. loops are polling loops. "Polling" is the process of repeatedly checking something. Since the loop code executes continuously, and because it is a small, "tight" loop, there is little time for the processor to switch tasks and do anything ...


Only top voted, non community-wiki answers of a minimum length are eligible