Event-driven programming

http://en.wikipedia.org/wiki/Event-driven_programming

http://www.cnblogs.com/alex-tech/archive/2011/10/27/2227058.html#_Toc127197014

http://en.wikipedia.org/wiki/Event-driven_architecture

事件驱动编程:程序的流程由事件(event)决定,这些事件包括:传感设备输出、用户动作(鼠标、键盘)、来自其他程序或线程的消息等。

事件驱动编程可以作为一种技术用于设计应用程序的架构。这时,应用程序就会有一个main loop,main loop由两部分组成:事件选择(或检测、监听,event selection/detection)和事件处理(event handler)。


在嵌入式系统中,用中断(interrupt)来代替main loop,此时的事件选择由硬件完成。


做上层应用的程序员主要工作是事件处理(handler)部分。事件的选择和main loop由程序框架来完成。




事件驱动编程实现步骤:


1.实现用于event handler的函数或方法,比如鼠标按下时将要执行的动作;
2.将handler函数与事件event绑定(handler函数的注册过程),这样当事件(如按下鼠标)发生时,就能触发handler,达到目的。
3.实现main loop。main loop函数的作用是检测事件的发生,并将事件与handler进行匹配,匹配成功将执行handler。大多数情况下事件驱动编程环境默认提供main loop,所以不需要我们自己编写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Event-driven programming is a programming paradigm that allows the software to respond to events or user actions in a asynchronous manner. Here are the general steps to implement an event-driven program: 1. Identify the events: Determine the events that the program needs to respond to. These could be user actions, system events, or external signals. 2. Define event handlers: Create functions or methods to handle each event. These functions will be executed when the event occurs. 3. Register event handlers: Associate each event handler with the corresponding event. This is usually done using a framework or library. 4. Run the program: Once the event handlers are registered, the program is ready to execute. As events occur, the associated event handlers will be executed. 5. Clean up: After the program finishes executing, clean up any resources used or allocated during the program. Here's an example of an event-driven program in Python using the Tkinter GUI library: ```python import tkinter as tk def button_click(event): print("Button clicked") root = tk.Tk() button = tk.Button(root, text="Click me") button.bind("<Button-1>", button_click) button.pack() root.mainloop() ``` In this program, we identify the event as a button click, define an event handler function `button_click` to handle the event, register the event handler with the button using the `bind` method, and run the program using the `mainloop` method. When the button is clicked, the `button_click` function will be executed and print "Button clicked" to the console.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值