VU at Work: The Recurse Project VB Help

You’ll find the Recurse application in this chapter’s folder on the CO:Load it and .run it. Click the New Color button and the program starts filling the picture box with a random color from left to right. Because the control’s ScaleMode is twips the progress ‘of the drawing is slow, even on a fast Pentium. The code behind  the  New Color Command button follows.

The New Color Button Code

Suppose the program starts filling the picture box with red lines. Before the program has a chance to complete its operation, click the New Color button again. The subroutine Commandl_ClickO is interrupted, and the program starts filling the control with a new calor, perhaps fuchsia. Interrupt the process again. This time, yellow kicks in and starts filling the control from left to right. Let this operation complete. .
As soon as the picture box is filled with yellow, the interrupted process continues. The program completes the drawing of the fuchsia lines, but it doesn’t start drawing from the left edge of the control It picks up from where it was interrupted. When the fuchsia color reaches the right edge of the control, red kicks in! Can you see what’s going on here? Each time you click the New Color button, the Commaiuil_Click() subroutine is interrupted, and a new copy of the same subroutine starts executing. The interrupted (or suspended) instance of the subroutine doesn’t die. It waits a chance to complete, which it gets when the newer instance of the subroutine completes its task.

This recursion is made possible by the DoEvents() statement placed in the loop’s body. Without it, you wouldn’t be able to interrupt the subroutine and invoke another instance of it. Nonnally, you wouldn’t use the DoEvents() statement to .avoid the very behavior you witnessed in this example. Most of the procedures you’ve written so far don’t use the DoEventsO statement; these procedures won’t allow another procedure to start executing before they have finished. I need to mention one important aspect of recursion here. The RGBColor variable is local and it maintains its value while the subroutine is interrupted. VISUalBasic stores the values of the local variables of the interrupted procedures and recalls . them when the procedure gets a chance to complete. This is possible &ecause each new copy of the procedure that starts executin as its own set of local variables. Local variables are part of the procedure’s status.

Posted on November 5, 2015 in Recursive Programming

Share the Story

Back to Top
Share This