Version 4 of event loop

Updated 2004-10-21 12:33:25

Purpose: Describe an event loop


Hopefully someone fills in the answers below!


What is an event loop? GWM a part of a program in which the program looks for events (mouse click, keyboard etc) and performs operations in response. Each operation should be short and self contained so that the event loop can quickly return to looking for the next event (long operations result in the application 'not responding' then suddenly doing lots of things when the slow event is processed). Most interactive applications use an event loop, including the browser you are reading this page through. A typical event is 'click mouse' - this sends the location of the mouse click to a procedure which detects 'what was clicked' (a Tcl button, menu item or scale etc) and calls a command that is associated with the button (etc).

Why would I use one? GWM When you want a program that you interact with. Typically when you are using Tk or any other GUI library.

How in Tcl do I program for an event loop? GWMUnless you have been too clever for your own good, you have already got an event loop. See Tcl_DoOneEvent.

What are some of the tricks in using an event loop? GWM make sure event checking is done regularly, at least every 0.1 seconds for good response. If you need a slow operation (read a large file for example) then either put up a 'wait....' type cursor or a '10-20-30% read display. Or break up the reading with Tcl_DoOneEvent() calls or run the slow operation in a separate thread.

What are some of the traps when programming an event loop? GWM Non- returning command issued from an event.

Where else should I read ? Tcl event loop


Make special mention of David Gravereaux's demonstration with TES of a safe way to combine the Tcl event loop with the Windows message pump (even in the presence of threading).