>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  User

Message Pump and Long-Running Sentences

Jwd (Jwdw as Windows j.exe and Jwdp as Java j.jar) is a normal message pump (event dispatch loop) program. User actions add messages to a queue (e.g., keyboard and mouse actions). Jwd takes the next message off the queue and runs the code for that message and continues this until the queue is empty and it goes idle waiting for new messages.

Some messages (activate, paint after uncovered, drop down menu, etc.) are handled completely by j.exe or j.jar code. Other messages (mouse click of a button, mouse move on an isigraph control) collect information about the form and event and call J with sentence  wdhandler_locale_''  . The wdhandler does  wd'q'   to get the event information and runs the appropriate J code.

The J Engine is used as a shared library and the execution of wdhandler is done in the same execution thread as the Jwd event handlers that are coded completely in C++ or Java. The J code is a direct extension of the C++ or Java code in j.exe or j.jar.

The enter key in an ijx window is an event just like a button click except that instead of running the wdhandler sentence it runs the sentence from the ijx line.

Running the J code in the same execution thread works well for sentences that run quickly and particularly well for sentences that handle frequent events (e.g., mouse moves) and use wd commands. The execution is simple, streamlined, and efficient.

Long-running sentences (>5 seconds) introduce some problems. Jwd processes each message in turn and can't process another message until the previous one is finished. During a long-running sentence Jwd will be unresponsive as messages such as repainting an uncovered window won't be processed until the long-running sentence ends.

To see this unresponsiveness run sentence  6!:3[10  (delay 10 seconds) and then move the window of another application over the Jwd windows. The windows won't look right until the delay finishes and messages to repaint can be processed in turn.

Very long-running sentences (e.g.,  6!:3[1000  ) can be a problem. See Interrupt Execution for how to interrupt execution and regain control of your session.

Sentences that take longer than 5 seconds raise some issues. An impatient user can poke things many times in 5 seconds. When the long-running sentence ends these actions are in the queue but probably don't represent what the user really wants and would be a mess if processed. When a J sentence ends Jwd does the following:
   if the sentence ran for more than 5 seconds
      disable forms and controls
      process queued messages
      enable forms and controls that were disabled

Messages for repainting etc. are processed and messages that would cause a wdhandler event are discarded as forms and controls are disabled.

Sometimes in a long-running sentence the programmer wants to be able to have messages pumped that cause normal window painting and updates (e.g., a status control) but not allow new wdhandler events. The wd command msgs runs a new message pump with all forms and controls disabled until the queue is empty. This allows screen updates and discards J events. Care must be taken as in addition to user keyboard and mouse actions, messages for sockets and timers are also be discarded.

An application that knows it will be nonresponsive for much longer than 5 seconds should probably hide windows other than a status window and possibly use wd'msgs' periodically to keep that window painted and updated.

An application with more extensive use of long-running sentences is probably best designed from the ground up to have the GUI part in one execution thread or task that is always responsive and the data processing part in a separate thread or task.

Normally messages are pumped only when Jwd is idle (no execution stack or state for either Jwd or the J Engine). There are 4 cases where messages are pumped during J execution:
   1!:1[1  (ijx keyboard input)
   debug suspension
   wd msgs command
   wd wait command
In these cases the J Engine calls back into Jwd to run a new message pump at that stack level.

>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  User