Jump to content

Event driven programming language

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gaius Cornelius (talk | contribs) at 18:27, 26 June 2006 (Delete duplicate word using AWB). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Template:Wikify-date

Event driven programming is a style of programming where the flow of the program is determined by user actions (mouse clicks, key presses) or messages from other programs. In contrast, in batch programming the flow is determined by the programmer. Batch programming is the style taught in beginning programming classes while event driven progrmaming 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 environment make the task quite easy, others less so.

Here are two pseudocode versions of a trivial program to add two numbers:

Batch version

read a number (from the keyboard) and store it in variable A[0]
read a number (from the keyboard) and store it in variable A[1]
print A[0]+A[1]

Event driven version

set counter K to 0
repeat {
   if a number has been entered (from the keyboard) {
       store in A[K] and increment K
       if K equals 2 print A[0]+A[1] and reset K to 0
   }
}

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.

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 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

declare globally the counter K and the integer T.
OnKeyEnter(character string S)
{
   convert S to a number N
   if K is zero store N in T and increment K
   otherwise add N to T, print the result and reset K to zero
}

Of course, using global variables 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 attenion 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.

The 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.