April 24, 2019, 03:02:25 am

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


4a. Messages and Message Handlers

Started by LarryMc, July 26, 2011, 08:21:23 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

Part A

Generally, in a console based program things progress in a predefined path from point A to point B.  The simplest and most straight forward programs  start, perform a series of tasks, and then stop.  Of course the program can be made more elaborate with loops and if-blocks.  But execution pretty much moves from instruction to instruction in a steady manner.  It is with these types of console programs, back in the DOS OS days, that we were exposed to our first hint of a rudimentary event driven program. Consider the two following examples:

Code Select
OPENCONSOLE
PRINT "Press any key to quit"
DO: UNTIL INKEY$ <> ""
CLOSECONSOLE
END


In the above example the program is in a loop waiting for an event to happen. The event is the pressing of any key on the keyboard.  To be more precise, the loop is waiting for any one of approximately 100 possible events to occur. That is because the pressing of a specific key is a different event then the pressing of any other key.

Consider the second example:

Code Select
OPENCONSOLE
PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"
CLOSECONSOLE
END


In this example we are in a loop until a single, specific event occurs; the pressing of the 'Q' key.  Let's expand this example.

Code Select
STRING A
OPENCONSOLE
PRINT "Press Q to quit"
DO
   A = INKEY$
   SELECT A
      CASE "A"
         MySubA()
      CASE "B"
         MySubB()
      CASE "C"
         MySubC()
   ENDSELECT
UNTIL A = "Q"
CLOSECONSOLE
END


Again, we are still in a loop waiting for an event to occur.  Think of INKEY$ as assigning an event identifier (message) to the event and placing that in the variable A.  The SELECT/ENDSELECT block is then used to respond to certain events by executing code specific to that event.  In this case, by calling a subroutine.

Two important things are happening in this example that must be understood.  The service provided by the INKEY$ function (identifying the events for us) is done without any regard to whether or not we will respond to that event. Also, we can pick which events we want to respond to.

The above is a very, very crude example of a message handler.  But it does demonstrate some important concepts. The main problem with the loop described above is that when that loop is entered there is nothing else anywhere in the application that can be executed.

Before proceeding, we need to clarify some terminology.  Message handler, event handler, and handler all refer to the same thing in the context of this tutorial.  Message, event id, notification, notification message all refer to the same thing; the identity of some event that has occurred.

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library