2

I have a lengthy process that I can't run asynchronously, so just like Explorer when you're copying files, I have a progress dialog. I show the dialog modally, then perform the operations (we'll call it copying files to keep it abstract), update the progress dialog, and pump messages with Application.DoEvents() to keep the application main frame painting and responsive.

Since the dialog is modal, the user isn't able to do anything other than watch or cancel. In other words, they can't select a menu item or click any buttons.

I've always believed Application.DoEvents to be extremely evil, because you could re-enter code you didn't intend to be re-entrant. But in this case, since the progress dialog is modal, I can't see a reason why this is a bad or dangerous solution.

Am I overlooking something, or is pumping messages with Application.DoEvents a legitimate thing to do with a modal dialog up?

Dave
  • 1,521
  • 17
  • 31
  • 1
    Why can't you run it at least in a background thread? Just use the Background worker, and update the UI using Control.BeginInvoke. – BFree Dec 11 '11 at 01:25
  • 1
    [Some](http://stackoverflow.com/a/5183623/825024) [reading](http://stackoverflow.com/a/1115410/825024). – Otiel Dec 11 '11 at 01:27

2 Answers2

0

I don't see a particular problem with this limited use of DoEvents. We successfully use it in very limited situations like this when background processing isn't an option.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

Normally, your UI thread in the application will pump messages. Now even though modal dialogs run their own message pump this is merely to keep the call behavior as expected (e.g. the call only returns when the modal dialog is closed), it is not a necessity for dealing with the modal dialog per se. (See also this blog post from "the old new thing" blog).

Therefore it isn't really a problem to call Application.DoEvents() either, even though it usually shows a design problem (e.g. non-cooperative processing in the UI thread) and it may cause re-entrancy if some window messages and event handlers are triggering each other - but that's independent of dialog modality.

Lucero
  • 59,176
  • 9
  • 122
  • 152