Event5_1.ZIP Asynchronous Execution - Example 1
The earlier projects showed how to start a long task in the Class module synchronously. They directly called the LongTask method of the Class object, which started a looping process. 

This project shows how to START a long task in the Class module asynchronously. I started with Event4_1.vbp, and separated it into two projects within one group. One project in the group contains the Form and command buttons, as before. The other project is an ActiveX DLL project, which contains a Form module in addition to the Class module from the earlier project. The Form holds a Timer control, which will eventually be used to start the LongTask method in the Class object.

Code Execution Here is the sequence of code execution in this example:
  1. Click on the "GO" button
  2. cmdGo_Click event ==>  moTest1.StartTask
  3. StartTask method of Class executes ==> shows msgBox, then Form1.Display Me
  4. Display method of Event5_1_SRV.frm executes ==>  Me.Timer1.Enabled = True
  5. Timer is enabled, code continues in Event5_1_CLI.frm ==> Call AddToList
  6. AddToList procedure executes until interrupted by Timer firing after two seconds
  7. Timer1_Timer event ==> disables timer, then moTester.LongTask
  8. LongTask method of Class executes ==> loops, calls RaiseEvent PercentDone(...)
  9. motest1_PercentDone event in Event5_1_CLI.frm fires
  10. Code returns to LongTask method of Class, continues to loop back to Step 8
  11. Eventually the LongTask method of Class object is finished
  12. AddToList procedure of Event5_1_CLI.frm executes until it is finished

It may be clearer to actually see it happen than read the above explanation. There are various messages that appear in the Immediate Window. The point is that the STARTING of the Class module code (the server side) is what is asynchronous. The code in Event5_1_CLI.frm (the client side) is executing happily, when it is interrupted by the LongTask method.

Experiment! There are three things that you can modify in this project for your amusement. These items are identified with rows of "asterisk comments" in the source code.
  1. In the AddToList procedure of Event5_1_CLI.frm, if you comment out the test on mbAllDone, the loop code will continue to run, even after you click the Exit button to close the form in the VB IDE. You'll also have to comment out this line in the For/Next loop:
    Me.List1.ListIndex = liNdex -  1
  2. In the AddToList procedure of Event5_1_CLI.frm, if you comment out the DoEvents statement in the For/Next loop, then the timer won't fire until after the For/Next loop is finished. This is an example of how a Timer control is not guaranteed to fire.
  3. In the motest1_PercentDone procedure of Event5_1_CLI.frm, if you un-comment out the line that re-enables the cmdGo button, you may see something strange. After the LongTask method finishes, and the AddToList loop is running, press the "GO" button again. After you clear the message box, you'll notice that the AddToList loop is running VERY SLOWLY. After two seconds, the timer will fire again, and the LongTask method will execute, and then the code returns to run the AddToList loop, VERY SLOWLY. I'm not sure exactly what is happening here.
Next try In the Event5_2 project, I modified the project using Windows API calls in an attempt to produce true asynchronous processing of the server component.