Event-driven Programming
Event-driven programming is a programming paradigm in which the flow of program
execution is determined by events - for example a user action such as a mouse click,
key press, or a message from the operating system or another program. An event-
driven application is designed to detect events as they occur, and then deal with
them using an appropriate event-handling procedure. The idea is an extension
of interrupt-driven programming of the kind found in early command-line
environments such as DOS, and in embedded systems (where the application is
implemented as firmware).
Event-driven programs can be written in any programming language, although some
languages (Visual Basic for example) are specifically designed to facilitate event-
driven programming, and provide an integrated development environment (IDE) that
partially automates the production of code, and provides a comprehensive selection
of built-in objects and controls, each of which can respond to a range of events.
Virtually all object-oriented and visual languages support event-driven programming.
Visual Basic, C#, Visual C++ and Java are examples of such languages.
A visual programming IDE such as Visual Studio provides much of the code for
detecting events automatically when a new application is created. The programmer
can therefore concentrate on issues such as interface design, which involves adding
controls such as command buttons, text boxes, and labels to standard forms (a form
represents an application's workspace or window). Once the user interface is
substantially complete, the programmer can add event-handling code to each control
as required.
Many visual programming environments will even provide code templates for event-
handlers, so the programmer only needs to provide the code that defines the action
the program should take when the event occurs. Each event-handler is usually
bound to a specific object or control on a form. Any additional subroutines, methods,
or function procedures required are usually placed in a separate code module, and
can be called from other parts of the program as and when needed.
Types of Events
User initiated Events can be triggered by users, such as by clicking on an icon or
entering some text
System initiated In automated systems, sensors can be used to detect events such
as when a particular temperature is reached in a glasshouse or a specific water level
is detected on a flood defence system.
GUI programming
It should not be assumed that because most popular modern software applications
have a graphical user interface (GUI) that event driven programming is the right
solution for every programming requirement. Some software systems have a very
specific role that involves them carrying out some task to completion with little or no
user intervention (a C compiler, for example). Such applications are probably better
served by a procedural programming paradigm. Having said that, most mainstream
commercial software relies heavily on the availability of a GUI, and most GUI
software is designed to be event-driven.
A visual programming language such as Visual Basic and Visual C/C++ now comes
with an Integrated Development Environment (IDE) that provides an extensive array
of standard controls, each with its own set of events and event-handler code
templates. The task of the GUI programmer is thus twofold – to create the user
interface, and to write the event-handler code (and any additional code modules that
might be required).
The IDE provides the scheduler and the event queue, and to a large extent takes
care of the flow of program execution. The GUI programmer is thus free to
concentrate on the application-specific code. They will write the code required by
each control or object to allow it to respond to a specific event, but do not need to
know how to create the objects themselves.