views:

368

answers:

6

Can someone please explain to me what is the difference between an event handler and a callback function?

+9  A: 

An event handler is a type of callback. It's called whenever an event occurs. The term is usually used in terms of user interfaces where events are things like moving the mouse, clicking something and so on.

cletus
So there are other scenarios when a callback could be called? Apart from events? Can you please give example of one such situation?
6pack kid
+3  A: 

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.

Carlos Gutiérrez
A: 

Event handler is a callback from the system.

Revenant
A: 

Generally speaking a 'callback' is under the controll 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 handlers queue and gets on with GUI managing, the event handler picks up the message from the queue, sees its a button push, fires up the "myaction" program, and moves on to handling the next event. Usually the "myaction" program will run as an idependent thread or even a separate process.

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

James Anderson
A: 

I love how all these answers differ from each other.

I would conclude from this, is that from an a terminology point of view Events and Callbacks are interchangeable. What they mean in a specific programming language or framework and differ though, because any platform tends to pick their favourite terms.

Evert
Not interchangeable; rather, an event handler is a type of callback. It is true, however that people some times use a very specific type of a thing to refer to the general type. A Kleenex is a tissue, but you wouldn't really say that you can use the words interchangeably, right?
Esteban Araya
A: 

Callback (from Wikipedia): "executable code that is passed as an argument to other code".
Event handler (again from Wikipedia): "asynchronous callback subroutine that handles inputs received in a program".

Which happens to be the way I've always understood it: an event handler is a very specific type of callback.

Esteban Araya