Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between event handlers and callbacks

What is the difference between an event handler and a callback function?

like image 886
Aaron S Avatar asked Jan 15 '10 05:01

Aaron S


People also ask

What is a callback handler?

public interface CallbackHandler. An application implements a CallbackHandler and passes it to underlying security services so that they may interact with the application to retrieve specific authentication data, such as usernames and passwords, or to display certain information, such as error and warning messages.

Is event listener a callback?

The event listener callbackThe event listener can be specified as either a callback function or an object whose handleEvent() method serves as the callback function.

What are event handlers?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place.

What is the difference between an event handler and an event listener?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.


2 Answers

A callback is procedure you pass as an argument to another procedure. The procedure receiving the parameter can call it, or share it so some other procedures in the system can call it.

An event handler is a procedure called when an event happens. It can be a callback.

like image 160
Carlos Gutiérrez Avatar answered Sep 29 '22 20:09

Carlos Gutiérrez


Generally speaking, a 'callback' is under the control of the detecting process. So you tell a GUI manager "call myaction when this button is pressed" and the GUI manager calls the action when the button is pressed.

Event Handlers on the other hand operate at one step removed. The GUI manager is configured to send messages to an event handler. You tell an event manager that button pushes are handled by the myaction program. When the button is pushed the GUI manager puts a message on the event handler's queue and gets on with GUI managing. The event handler picks up the message from the queue, sees it's a button push, fires up the myaction program, and moves on to handling the next event. Usually the myaction program will run as an independent thread or even a separate process.

While the "event handler" pattern is more complex it is much more robust and less likely to hang when an action fails. It also makes for a more responsive GUI.

like image 28
James Anderson Avatar answered Sep 29 '22 20:09

James Anderson