事件处理程序和回调之间的区别 [英] Difference between event handlers and callbacks

查看:28
本文介绍了事件处理程序和回调之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件处理程序和回调函数有什么区别?

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

推荐答案

一般来说,回调"是在检测过程的控制之下.因此,您告诉 GUI 管理器按下此按钮时调用 myaction",然后 GUI 管理器在按下按钮时调用该操作.

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.

另一方面,事件处理程序在一个步骤中运行.GUI 管理器被配置为向事件处理程序发送消息.您告诉事件管理器按钮按下由 myaction 程序处理.当按下按钮时,GUI 管理器在事件处理程序的队列中放置一条消息并继续进行 GUI 管理.事件处理程序从队列中获取消息,看到它是一个按钮按下,启动 myaction 程序,然后继续处理下一个事件.通常myaction 程序会作为一个独立的线程甚至一个单独的进程运行.

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.

虽然事件处理程序"模式更复杂,但它更健壮,并且在操作失败时不太可能挂起.它还使 GUI 响应更快.

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.

这篇关于事件处理程序和回调之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆