Question

If I'm looping through a prolonged operation (say, processing files) and I want to update a progress bar, I need to use DoEvents, from what I can understand.

But calling it during every loop of the function only results in the progress bar's animation being played back really fast (or slow, depending on the operation). I understand that this is because DoEvents allows the progress bar to "breathe", for lack of a better word, causing it and the rest of the form to refresh.

My question is, how do you know that it's appropriate to call DoEvents? Obviously you can't just call it on a whim however-often you feel like it - this results in sporadic animations, among other things. So is there some quick method to check if a form/application needs a DoEvents called?

Was it helpful?

Solution

Oh, duh - I just smacked myself.

I should just use a background thread to process, and leave the UI thread alone.

OTHER TIPS

If you don't want to fiddle around with threads and you don't mind a little beta-testing, the Visual Studio Async CTP might be worth a look -- it's basically "DoEvents done the right way" (and more).

For an introduction to these new async features, I recommend to check out Eric Lippert's blog.

Instead of messing around with threads, you can use a BackgroundWorker. It simplifies performing work and updating controls on a Form as you cannot update a UI Control directly from a non-UI thread. The example in the docs updates a Label control, but you can easily modify it to use a ProgressBar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top