Event (programming)

from Wikipedia, the free encyclopedia

An event ( English event ) is used in the software art - by development after the event-oriented programming paradigm  - to control the program flow . The program is not run through linearly, but special event handling routines ( listener , observer , event handler ) are always executed when a certain event occurs. Event-oriented programming is one of the parallel programming techniques , so it has its advantages and disadvantages.

motivation

A common case in programs is that resource requests (such as memory requests and other device accesses) have to be waited for an unpredictably long time or events can occur spontaneously (such as mouse clicks by the user). With older programming, without events (or with a single, sequential control flow ), this "waiting" for the occurrence of the event (e.g. successful resource allocation) is carried out via active, observing waiting (also known as polling ).

Polling has some disadvantageous properties: Since program execution is (apparently) halted until the event occurs, program performance is poor and response times are also unpredictable. It is also not possible to react to other events during the observing wait, i.e. i.e., they may be lost. And the execution of the observing wait requires an unpredictably large amount of computing time , since the same action - the check whether the event has occurred - has to be repeated unpredictably often.

Event-oriented programming

One approach to solving this problem situation more efficiently is event-oriented programming , which is based on an inversion of control . This means that the main control flow no longer waits for the event to occur (the main control flow is in control), but rather the event is assigned its own control flow (often implemented as a thread ), which becomes active independently when the event occurs and can influence the main control flow (see parallel programming ).

Technical implementations of this idea since the 1960s are the callback function (with event-specific subroutines ) and (hardware) interrupts , which avoid the disadvantages of polling, but inevitably cause the potential problems of parallel programming.

Event handling techniques can also be described in design pattern terminology as an observer .

use

The concept of event-oriented programming is also well suited for the implementation of graphical user interfaces , with the events here mostly being actions by the user , such as pressing a key or clicking a button . Another important field of application are computer simulations , which are set up in such a way that changes of state are only triggered by events, and in turn trigger events (see event-oriented simulation ).

Event- oriented programming can be easily combined with the concepts of object-oriented programming (OOP): Objects then no longer only define properties and methods , but are also event sources and offer the possibility of influencing event handling. The event handlers ( english event handler , German  event handler ) and the events themselves are then modeled as objects. However, it can be argued that, via the idea of ​​decoupled messaging between object entities, event-oriented programming has always been implicitly a sub-concept of OOP.

Depending on the programming environment, events can either call just one event handler (such as in Object Pascal ) or any number of event handlers (such as in Visual Basic , C # or as in the signal slot concept ).

It is possible to mark an event as "processed" (consume). Subsequent event handlers can query this and then dispense with further processing.

Examples

Example for MS Access

The user can u. a. Design forms and reports with fields in each. In addition, there are 'areas' such as form header, report header, group header and group footer (for each group level) and detail area, which themselves contain individual fields. All of these concepts are objects .

The processing for objects is divided into functional sub-units, the execution of which depends on the occurrence of certain events ("event-oriented"):

Defining Events in MS Access (2003)

In forms , the events mainly occur through actions of the user interface: mouse actions, inputs, etc. - which are recognized and handled by the Access Engine . Possible events are (for example):

  • for forms: open, display, before input, changed, delete, close ...
  • For form input fields: if changed, when touching with the mouse pointer, when clicking, when double-clicking, with the UP key
  • for command buttons: when going, when clicking, when double-clicking

In the case of reports , the engine initiates the events depending on the data, similar to the control principles of standardized programming . Possible events are (for example):

  • for the entire report: when opening / closing, at the top of the page, with empty data
  • for report areas such as group header and footer: when printing, when formatting

In addition to the standard processing of each event type by MS Access, the programmer can determine for each object and for each event type whether something individual needs to be done - and what. For example , a certain check can be made after changing an input field; when opening a report in the case of 'empty data', an error message may be displayed; a group footer can be made 'invisible' with only 1 single line per group; Data fields can be made visible / invisible or output with specific content.

For such additional functions, the programmer, with the support of a software "wizard" if necessary, creates a procedure in which a suitable code (in VBA ) is stored for the respective object and the respective event type ; see graphic example. When the event occurs, the corresponding procedure is carried out. If no procedure is created, the event is processed in the defined basic form or there is no processing (e.g. when the object is touched with the mouse pointer ).

Implementation of an event system

The following pseudocode is intended to show a simple implementation of an event system:

Function Event
  listener = []
  call = function()
    for each parallel (l in listener)
      l()

Application example:

Klick = new Event
Klick.listener.add(regenGeräusch)
Klick.listener.add(regenBild)
Klick()

This simple event system offers event handling in a linear manner and enables event handling routines to be logged on and off. The W3C plans so-called web workers for parallel execution . The event system shown can be used as follows:

Formular = function() {
  this.abschicken = new Event();
  
}

function zumServerSenden() {
  
}

function DankeSagen() {
  alert("Vielen Dank für das Ausfüllen des Formulars.");
}

var umfrage=new Formular();
umfrage.abschicken.addListener(this, "zumServerSenden");
umfrage.abschicken.addListener(this, "DankeSagen");
umfrage.abschicken();

See also

Individual evidence

  1. Stefan Ram: Dr. Alan Kay on the Meaning of Object-Oriented Programming ( English ) fu-berlin.de. July 23, 2003. Retrieved June 4, 2012: " OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. "