Sun. Jan 22nd, 2023

Event Driven Programming – Features

This is a list of features of event driven programming, along with descriptions.

Event

An event is an action that can be reacted to. Examples of events include mouse clicks, key presses, data input from sensors, messages being sent from other programs and so on.

Event Handler

The event handler is the block of code that is intended to run in response to an event. Say you want a message to appear when a button is clicked: the button being clicked is the event, and the event handler is the code that makes the message appear.

Event Loop

The event loop is a queue data structure, and it contains a list of events that have been raised, along with any relevant data (for example the x and y coordinates of a mouse click).

Messages are removed from this queue in the order in which they are received – that is, the first task added to the list will be the first task taken off and processed.

The event loop is an operating-system supplied piece of code that loops round, checking the message queue, and processing messages when they arrive.

Service Oriented Processing

Service Oriented Processing refers to background services running on a system, that are awoken by events. The easiest example to understand is the USB device recognition service on a modern PC. When a USB device is attached or removed, an event is raised and dispatched to the software service, which enables interaction to determine, amongst other things, what action to take with the new device.

Points to clarify this:

  • There is no point in scanning the USB devices constantly. You only need to know when the attached devices change
  • Services refer to software programs that run in the background but without a user interface. They perform background tasks.

Time Driven

Time-driven means that the system is sensitive to time. This is easiest to comprehend with an example – perhaps an alarm clock.

An alarm clock needs to check whether the time is a certain value, but it only needs to check relatively infrequently, for example once a second. WE have code that needs to run once per second: it’s time driven. For this, we can use a timer that raises an event (e.g. TimeElapsed) that we can react to.

Trigger Function

Trigger functions are the glue that match events to event handlers. Consider the code below:

C#

//Assume there's a button called ShowMessage

//Click is the event we are interested in
//The line below is the trigger function. It ties together
//that the click event should result in the 
//ShowMessageClicked event handler running
ShowMessage.Click += new ShowMessageClicked;

//Here is the event handler - the code that does 'something'
void ShowMessageClicked(object sender, EventArgs e)
{
  MessageBox.Show("This is a message");
}