Page 55 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 55
Chapter 3
Notice that if the DoEvents call is not included in the loop, then the loop will not release the
CPU, and the query form will not receive any CPU time. As a result the events of the
operator clicking any of its response buttons will never fire, and the whole program will lock
up waiting for something that will never happen.
In this example, if any response other than a yes response is returned, then the
program terminates and takes no action. This is all quite clear, but what is the
variable Unloading all about?
Freddy as a VB form
th
In the famous thriller Friday the 13 and its many sequels, the demented killer
Freddy refuses to stay dead and continues to pop up to cause mayhem. VB can create
forms and functions that use Freddy as their role model. The problem usually occurs
in MDI (Multi-Document Interface) applications.
An MDI consists of a main form with a menu at the top, and it has any number of
daughter forms that may appear within the main form’s work area. Microsoft Word is
an example of an MDI. The MDI format is ideal for real-time controls that have
multiple forms.
The problem comes when a well-behaved MDI daughter form or function calls
DoEvents, and the event that occurs is a closure of its Main form. Closing the Main
form automatically closes any of its open daughter forms and functions, but this
means that the very program thread that called DoEvents is gone when the operating
system attempts to return into it after executing the closure! This situation will
crash the computer, so the operating system resurrects the snippet of code or the
form that called DoEvents. This results in either the form popping back up after the
parent MDI is gone, or in the case of a function it may simply resurrect an invisible
chunk of code and allow it to continue to run invisibly. In some cases, it may even
resurrect the MDI Main form.
This bug is present in a lot of shareware programs, and not a few commercial appli-
cations. After you close the program, if you look at your system resources, you will
see that a part of it is still running. Even if there is no visible element left open, the
running code absorbs machine cycles (resources), and can cause problems when
shutting down Windows.
38

