Updated 2014-07-13 03:39:33 by RLE

Purpose: explain the fundamentals of event programming for someone new to the concept.

Many modern programming tasks involve the integration of things that happen [asynchronous]ly; they do not happen at precisely controlled times and the computer has to be prepared to deal with whatever happens in the order that it happens.

For example, in a GUI you don't want to force a user to press a button between each move of the mouse, and in a network server with two clients connected you definitely do not want to force one client to wait for the other to do something (unless you're doing something collaborative of course! :^)

The most commonly used technique for doing this is called event-based programming, and it is such a good coding idiom that it is used in nearly every practical programming language in use today. Of course, some languages offer better support for it than others...

The basic idea is that you have a queue of possible events, and as the environment (i.e. the world outside the program) does things, so events are generated and added to the queue. Meanwhile, the program sits there, grabbing events off the queue and doing whatever it takes to deal with them, usually by way of a gigantic [switch] statement (or whatever that language's equivalent is.)

Luckily, Tcl comes with all this support built in already. All it takes to turn it on is to set up some event handlers and to start accepting events using the [vwait] command. In Tk, things are even easier as the library sets up a load of handlers anyway, and also starts up the event loop once it has evaluated your startup script.

People who aren't used to event-driven programming often have a hard time getting started. See Countdown program for a simple example of converting a loop-based program to an event-driven one.

Major classes of events in Tcl/Tk

  • GUI events - Tk's raison d'etre. Note that this includes send and various wm subcommands as well as bind and the -command options to various widgets...
  • File events - These occur when it becomes possible to either read from or write to a channel (especially to a pipe, socket or serial port.)
  • Timer events - Great for scheduling things for happening later, and brilliant for use with periodic activity.
  • Idle events - Useful for when you wish to put something off until there is nothing to do except wait for events (mainly used in the management of display updates...)
  • Virtual events - Comes in handy when a certain program state which is reached should call a proc at a higher abstraction level of the application program. It is not good practice to call a high level proc from a lower level. event generate is helpful because (a) the programmer specifies a name for the virtual event (hence a low level software module is consistent in having defined names for ALL internal resources which can be used by apps) and (b) the event can simply be ignored when not needed.

There are also conditions that are internal to a program that can be best thought of as being events:

Caveat: there is a difference between these actions, and the 'real' events listed above. Those events happen when the event loop is entered, but the ones listed here happen directly in the processing of the current code. To see what I mean compare when traces on variable writes occur vs. a section of code doing a vwait on the same variable.

An even easier realization of asynchronous processing for programmers is BLT's bgexec.

What would it take for Tcl's event handling to support user defined events? Perhaps I want to generate events based on encounters of particular tokens during input file parsing, etc. Does this kind of thing currently event? Can one bind to these events so that code gets called automatically upon the raising of a user event?

KBK (9 August 2001) - User defined events exist in Tk, but they're limited to UI components; see the event manual page, with particular attention to [event generate]. It would be nice to have event bindings on other sorts of things. Michael McLennan's keynote speech at the 2001 Tcl conference put forward a proposal for non-GUI event definitions. Is that on the Web anywhere?

Yes. http://conferences.oreillynet.com/pub/w/10/tcl_presentations.html (That appears to have his paper - but not any of the material from his keynote speech, Dealing with my Tcl addiction, which is where he brought up the notification system with which he and George Howlett had been experimenting.)

trigger is mentioned in one paper - see that page for the PDF of the paper. However, I've not seen where the code itself is available.

2010-12-14 A TIP to address this issue is under discussion: TIP #379

User defined events for Tcl can be had in the uevent package from tcllib. It provides support similar to the Tk event command.