> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How DoEvents Works: The Nitty-Gritty Technical Details
- URL: https://nolongerset.com/how-doevents-works/
- Published: 2021-07-24T01:25:25.000Z
- Updated: 2026-05-08T13:03:29.000Z
- Description: A peek behind the scenes at the inner workings of the DoEvents function.
- Author: Mike Wolfe
- Tags: DoEvents, Advanced, #Import 2026-05-20 02:59

This is part 5 of a [multi-part series](https://nolongerset.com/demystifying-doevents/) on the DoEvents function.

---

I have a confession to make. I wrote an entire five-part series of blog posts just so I had an excuse to quote the two paragraphs below.

Several weeks ago, I tumbled down some esoteric VBA/VB6 rabbit hole on the interwebs. As I struggled to make sense of a concept soaring over my head, a comment caught my eye. In explaining this complex topic, the comment poster referenced the book *[Advanced Visual Basic 6](https://www.amazon.com/Advanced-Visual-Basic-Techniques-Everyday/dp/0201707128)*, by Matthew Curland.

As I've come to learn, finding insights into deep topics in VBA is surprisingly difficult (it's part of the reason I started this blog). Since VBA and VB6 are essentially the same language (*as opposed to VB6 and VB.NET, which are very different languages that just happen to share a name and a great deal of syntax*), I've found that the best way to get information on advanced VBA topics is to buy decades-old books about VB6.

From page 308 of *Advanced Visual Basic 6*:

> Visual Basic provides the DoEvents function for arbitrarily clearing pending messages in all the current thread's windows. Since pending methods masquerade as messages, calling DoEvents lets them through. The problem is that DoEvents lets everything else through as well. You get a flood instead of a trickle. If you want only the trickle, you'll have to use something other than DoEvents.  
>  
> DoEvents first clears all pending keystrokes from the SendKeys function and then enters a loop that clears all messages with the PeekMessage API. Finally, DoEvents calls the Sleep API to release the remaining time slice on the current thread.

Here's a summary of the text in bullet-point form:

- Clear all pending keystrokes from the [SendKeys function](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement)
- Clear all [window messages](https://nolongerset.com/why-doevents-works/#whats-a-windows-message) with the [PeekMessage API](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea)
- Call the [Sleep API](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep) (probably with an argument of 0)

## 

### External references

[Advanced Visual Basic 6: Power Techniques for Everyday Programs: Gary Clarke, Curland, Matthew: 9780201707120: Amazon.com: BooksAdvanced Visual Basic 6: Power Techniques for Everyday Programs \[Gary Clarke, Curland, Matthew\] on Amazon.com. \*FREE\* shipping on qualifying offers. Advanced Visual Basic 6: Power Techniques for Everyday ProgramsGary Clarke![](https://images-na.ssl-images-amazon.com/images/G/01/books/editorial/ABR/celebrity_picks/2021-7/HQP-355x70._CB665663370_.jpg)](https://www.amazon.com/Advanced-Visual-Basic-Techniques-Everyday/dp/0201707128)

[SendKeys statement (VBA)Microsoft Docso365devx](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement)

[PeekMessageA function (winuser.h) - Win32 appsDispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).Microsoft Docsjwmsft![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea)

[Sleep function (synchapi.h) - Win32 appsSuspends the execution of the current thread until the time-out interval elapses.Microsoft Docskarl-bridge-microsoft![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep)

### Referenced articles

[A High-Level Overview of the DoEvents Function CallWhy does calling DoEvents save us from the dreaded ”(Not Responding)” treatment? It’s all about handling “Windows Messages.”![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/07/architecture-1837176_1920.jpg)](https://nolongerset.com/why-doevents-works/#whats-a-windows-message)

*Image by [Pavlofox](https://pixabay.com/users/pavlofox-514753/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1452986) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1452986)*