开发者

When do I know to call DoEvents?

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 开发者_JAVA技巧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?


Oh, duh - I just smacked myself.

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜