How to Teach a Six-Year Old About the DoEvents Function

I challenge you to read this article without laughing or learning anything new about the DoEvents function.

How to Teach a Six-Year Old About the DoEvents Function

This is part 1 of a multi-part series on the DoEvents function.

What is DoEvents

Let's start with the official explanation:

Yields execution so that the operating system can process other events.

Let's unpack this definition piece by piece.

  • Yields: give someone (or something) else a turn
  • execution: in this context, execution can be read as "processor time"
  • so that the operating system: aka, Windows
  • can process: "can deal with"
  • other events: anything happening outside of the current procedure or the calling procedures (i.e., the call stack)

Explain It to Me Like I'm 6 Years Old

DoEvents is how you make your code play nicely with other applications.  It even helps your code play nicely with your own application.

The school metaphor

Let's pretend the computer is a school.  Each classroom is a different application.  Students are assigned tasks.  Every task involves walking in the halls, but there is only one hall pass.

You, the programmer, are the teacher.  It's your job to manage the students and their tasks.

Food drive

The school just finished up its annual food drive.  Students brought donations of canned goods into their classrooms.  The classroom that collected the most food gets a pizza party.

The students in your classroom brought in a hundred cans of beans.  You now have to get these cans to the library, which is the central collection point.  

You give Billy the task of taking the cans to the library.  Billy is scrawny; he can only carry one can at a time.  

You put Shannon in charge of updating the bulletin board with the number of cans Billy has taken to the library.

You give Billy the hall pass and send him to the library with the first can.  Billy comes back, gets the next can, and heads back to the library.  

Shannon tries to update the bulletin board, but Billy has the hall pass.  She can't leave the room.

Billy comes back for the next can.  He grabs the can and immediately heads for the library.  Billy comes back and forth a dozen more times.  Poor Billy is really working up a sweat.  

Meanwhile, the other kids are getting restless.  The classroom across the hall shows 9 cans delivered on their bulletin board.  Your bulletin board is still empty because Shannon hasn't had a chance to get the hall pass from Billy.

Billy keeps walking back and forth.  One can at a time.  The count keeps increasing, but Shannon hasn't been able to update the bulletin board.

Not responding

The principal looks out from the window of the main office.  All the other classrooms have updated their can count, but yours still shows zero.  Concerned, he asks one of the aides to call your classroom to see what's going on.

You can hear the phone ringing.  You ask José to answer it.  Like everything else, though, the phone is in the hallway.  José gets in line behind Shannon to wait his turn for the hall pass–the hall pass whose ink is starting to smear in Billy's sweaty little fist.

The phone rings several times and then goes quiet.  The aide hangs up the phone and says to the principal, "I called but they're not responding."  

Cancel the process

Red-faced, the principal demands to know what's going on in your classroom.  "I think Billy is the one bringing the cans to the library," says the aide.  "It might just be taking him awhile."

The principal mumbles something about Billy's inability to fight his way out of a wet paper bag, then bellows to the aide, "Tell that classroom to stop what they're doing."  The aide presses a button and a light turns red inside the classroom.

Kedisha sees the red light and jumps up out of her seat.  "The office has a message for us and I'm on courier duty today!  I can't wait to find out what it is!"  Kedisha bounds to the front of the classroom...and gets in line behind José.

Meanwhile, the classroom across the hall is in full celebration mode.  Their bulletin board shows 75 cans of food delivered, way more than any other classroom.

Process complete

Dripping with sweat, Billy trudges back after delivering the last can.   He transfers the soggy wad of paper that was once a hall pass to Shannon.  She skips out into the hallway and slaps a huge "100" on the bulletin board.

Joy turns to disappointment across yonder hallway, as the rival class sees their pizza party dreams vanish.

Shannon twirls back into the room, tossing the hall pass wad to José.  José runs into the hallway and picks up the phone.  The line is dead.  Confused and disappointed, he shuffles back to the classroom.  He forgets about the hall pass he has balled up in his fist.  As he walks by, Kedisha pries it loose before sprinting to the office.

Kedisha appears back in the classroom a few seconds later, breathing heavily.  "What's the message from the office?" you ask her.

"They said to stop what we're doing," Kedisha chirps.  "I don't know what they're talking about.  I think we're all done now."

"Indeed we are, Kedisha.  Indeed we are."

I was told this was an article about the DoEvents function

OK, it took me awhile, but we're finally to the part about DoEvents itself.  Let's go back to the official definition:

Yields execution so that the operating system can process other events.

Lets break this down again using our example:

  • Yields execution: Billy gives up the hall pass
  • so that the operating system: you (the teacher)
  • can process other events: give the hall pass to each of the other kids in line

In other words, calling DoEvents in your code would be like taking the hall pass from Billy, giving it to the next kid in line, and telling Billy to go stand in the back of the line.

Referenced articles

Demystifying DoEvents
The DoEvents function may be the single most misunderstood piece of code in the entire VBA language. In this series of articles, I’ll try to remedy that.

Image by Bessi from Pixabay

All original code samples by Mike Wolfe are licensed under CC BY 4.0