事件侦听器如何工作? [英] How do event listeners work?

查看:120
本文介绍了事件侦听器如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是否反复检查条件并执行是否满足条件.例如,操作系统如何确切知道何时插入USB设备,或者MSN如何确切知道何时收到电子邮件.如何运作?

Do they repeatedly check for the condition and execute if the condition is met. Ex, how the OS knows exactly when a USB device is plugged in, or how MSN knows exactly when you get an email. How does this work?

谢谢

推荐答案

在底层,操作系统内核会知道"发生什么事情,因为相关设备向CPU发送了硬件中断.

At the low level, the OS kernel "knows" when something happens, because the device in question sends the CPU a hardware interrupt.

因此,例如当网络数据包到达时,网络控制器将发送一个中断,并且OS内核将做出适当的响应.

So when, say a network packet arrives, the network controller sends an interrupt, and the OS kernel responds as appropriate.

在程序级别,它的工作方式大不相同-大多数应用程序运行一个事件循环",从中获取一条消息(例如,操作系统发出的一条消息,指出在应用程序的这一点上单击了鼠标" ),对此进行相应的操作,然后侦听更多消息.如果没有消息,则操作系统将休眠线程,直到有消息要传递为止.

At the program level, it works quite differently - most application programs run an "event loop", where they fetch a message (say, a message from the OS saying that "the mouse was clicked on this point in your application"), perform the appropriate actions in response to that, and then, listen for more messages. If there is no message, the OS sleeps the thread until it has a message to deliver.

这篇关于事件侦听器如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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