Matthew MacDonald
1 min readJun 26, 2021

--

Absolutely, but the problem with DoEvents() is that you don’t know what code is going to run when you yield. Other code in your application may be triggered, which is the source of most of the headaches. Even Microsoft gives us a big note of caution.

There are better solutions. You could spin up a separate thread to do its work happily on the background thread and then marshal itself to the UI thread when it’s finished, but you don’t even need to do that. With BackgroundWorker, you’ll get a completion event to handle. It feels like normal control-based event logic, but your worker code is automatically run on a pooled background thread, giving you all the responsivity you want and none of the headaches. Of course, you can use DoEvents() safely if you disable other controls/windows in your application that might generate more messages, but that’s on you. Thanks for commenting!

--

--

Matthew MacDonald
Matthew MacDonald

Written by Matthew MacDonald

Teacher, coder, long-ago Microsoft MVP. Author of heavy books. Join Young Coder for a creative take on science and technology. Queries: matthew@prosetech.com

Responses (1)