| Event-driven Programming |
Article Index for Event-driven |
Website Links For Programming |
Information AboutEvent-driven Programming |
| CATEGORIES ABOUT EVENT-DRIVEN PROGRAMMING | |
| programming paradigms | |
| event computing | |
|
Event-driven programming or '''event-based programming''' is a Computer Programming Paradigm in which the Flow Of The Program is determined by user actions ( Mouse clicks, key presses) or Messages from other programs. In contrast, in Batch Programming or Flow-driven Programming the flow is determined by the programmer. Batch programming is the style taught in beginning programming classes while event-driven programming is what is needed in any interactive program. Event-driven programs can be written in any language, although the task is easier in some languages than in others. Some Programming Environments make the task quite easy, others less so. OVERVIEW Instead of waiting for a complete command to make it process information, the system is preprogrammed with an Event Loop , to look repeatedly for information to process (whether this might be the appearance of a file in a folder, a keyboard or mouse operation, or a timer event) and then perform a '''trigger function''' to process it. Programming an '''event-driven''' system is thus a matter of rewriting the default trigger functions of the system, to match the required behavior. The method by which information on events is acquired by the underlying system is immaterial. Inputs can be Polled in the event loop, or Interrupt Handler s can be registered to react to hardware events; many systems use a mixture of both techniques. The preprogrammed algorithm ensures that triggers provided are executed when they are needed, thus providing a software abstraction that emulates an interrupt driven environment. Event-driven programs typically consist of a number of small programs called Event Handler s, which are to be called in response to external events, and a Dispatcher , which calls the event handlers, often using an Event Queue to hold unprocessed events. In many cases event handlers can trigger events themselves, possibly leading to an Event Cascade . Event-driven programming stresses flexibility and Asynchrony as virtues, and tries to be as Modeless as possible. Graphical User Interface programs are typically programmed in an event-driven style. Computer Operating System s are another classic example of event-driven programs on at least two levels. At the lowest level, Interrupt Handler s act as direct event handlers for hardware events, with the CPU hardware performing the role of the dispatcher. Operating systems also typically act as dispatchers for software processes, passing data and Software Interrupt s to User Process es that in many cases are programmed as event handlers themselves. A Command Line Interface can be viewed as a special case of the event-driven model in which the system, which is inactive, awaits one very complex event – the entry of a command by the user. . Event-driven programming is a flexible way to allow your programs to respond to many different inputs or events. CONTRAST WITH BATCH PROGRAMMING Here are two Pseudocode versions of a trivial program to add two numbers: Batch version
Event-driven version
At first sight, the event-driven program seems more cumbersome and for such a trivial task is indeed so. However, the second program can be made generalized far more easily than the first. Instead of checking just for a number entry we may add code to check whether any of several events has occurred. Then for each event we can execute a particular piece of code that is commonly referred to as an Event Handler . EXAMPLE: READING FROM A SOCKET This example uses Pseudocode to illustrate how data is read from a Socket using an event-driven approach: function read_next_data(fd) data = read_async( fd ) if len(data) == 0 => Nothing to read, register to be called back when something is ready event_polling_register( fd, read_next_data ) => Go back to doing something else else => Data was available and len(data) was received add_data_to_buffer( buffer, data ) end_if end_function This example uses Tcl code to illustrate how data is read from a socket using an event-driven approach: # open channel set chan [socket ] set buffer "" fconfigure -blocking none # register event handler fileevent readable read_next_data buffer # process event until end of file proc read_next_data {chan bufferVar} { upvar #0 buffer append buffer if { } {close } } EVENT HANDLERS Because the code for checking for events and the Main Loop do not depend on the application, many programming frameworks take care of their implementation and expect the user to provide only the code for the event handlers. In this simple example there may be a call to event handler called OnKeyEnter() that includes an argument with a string of characters, corresponding to what the user typed before hitting the ENTER key. If we want to add two numbers we need to use storage outside the event handler, so the implementation might look like this A trivial event handler
Of course, using Global Variable s is not a good idea and a better solution is to use Object Oriented Programming making the event handler a method of an object that also holds the information necessary between calls to the event handler. While keeping track of history is straightfoward in a batch program, it requires special attention and planning in an event-driven program. CREATING EVENT HANDLERS The first step in developing an event-driven program is to write a series of Subroutines , or Methods , called event-handler routines. These routines handle the events that the main program will respond to. For example, in a GUI program, we might be interested in a single (as opposed to a double) left-button mouse-click on a command button. So a routine would be written to respond to such an event. The routine might open another window, save data to a Database or exit the application. Many modern day programming environments provide the programmer with event templates so that the programmer need only supply the event code. BINDING EVENT HANDLERS The second step is to bind event handlers to events, so that the correct function is called when the event takes place. Graphical editors combine the first two steps: double-click on a button, and the editor creates an (empty) event handler associated with the user clicking the button and opens a text window so you can edit the event handler. MAIN LOOP See Also: main loop The third step in developing an event-driven program is to write the Main Loop : a function that checks for events, and then calls the matching event handler. Most event-driven programming environments already provide this main loop, so it need not be rewritten. FRAMEWORKS AND LIBRARIES
SEE ALSO
REFERENCES
EXTERNAL LINKS
|
|
|