0

So I need to do heavy interface operations in my program... More specifically, populating a Windows Forms list view and generating images on the fly. This cannot be done on another thread.
So how do I show a modal, working (responding) dialog over my main WPF window?
The dialog can be either WPF or winforms, I don't care, I'll adapt...

It must be usable and always stay above and modally block it's owner form.

Vercas
  • 8,931
  • 15
  • 66
  • 106
  • What exactly is the operation you think you can't do on another thread? –  Apr 17 '11 at 17:07
  • @jdv-Jan de Vaan: When you update a Control, you must do it from the same thread as the Owner, unless you follow a particular procedure I can't remember well, but it's not to hard to still do it. Here's a reference from another question: http://stackoverflow.com/questions/1136399/how-to-update-textbox-on-gui-from-another-thread-in-c – Eugenio De Hoyos Apr 17 '11 at 17:10
  • It can be done on other threads. The update of the UI cannot, of course. But one is not the other. –  Apr 17 '11 at 17:14
  • @Eugenio: I know that. I was asking the OP. –  Apr 17 '11 at 17:45
  • @jdv-Jan de Vaan: OK. I provided the info for reference to the asker... I also wanted to refresh my mind... – Eugenio De Hoyos Apr 17 '11 at 17:52
  • What am I doing in the same thread? Did you read my question? – Vercas Apr 17 '11 at 20:10

3 Answers3

2

Just implement a dialog and call it using .ShowDialog() inside your time-consuming function. Wherever you have your time-consuming loop, call Application.DoEvents() every once in a while to allow the dialog box to show and update. Careful with this approach, though...

Be careful because if, for instance, you are handling a Paint event, and then you call Application.DoEvents() you might enter the Paint event handler twice, which is not desirable.

Eugenio De Hoyos
  • 1,755
  • 16
  • 22
1

On a WPF window or WinForm place an image and insert this: http://www.hindustantimes.com/images/loading_gif.gif which will automatically get animated. Show the windows using .ShowDialog(OwnerWindow) thus it will always remain on top of it's owner windows.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
0

If you are fine with calling Application.DoEvents and it works, then that is surely one approach. However, in my experience, Application.DoEvents is most always a hack and rarely a good idea.

My first thought, is perhaps you can create a background thread which generates your images. Begin your thread and show your modal dialog. For your thread, after it's created a certain amount (e.g. 5, 10, 20, 50, whatever), it would Invoke to the event dispatcher and execute a method which updates the ListView with the new batch of Images. Because it was only invoking periodically it would give the Modal Dialog time to process messages of its own. The function which you invoke on the event dispatcher would receive a list of images, Call ListView.BeginUpdate(), Loop through the list and create and add items, then call ListView.EndUpdate()

There's other ways you can do this, but I would really try to avoid Application.DoEvents() (which in my experience is a hack coming from Visual Basic programmers who did not understand threading).

Alan
  • 7,875
  • 1
  • 28
  • 48
  • You could even add a Stopwatch to your background thread and have it update the List every so many seconds instead of counting images. It seems like a bad idea to update your gui on every image (lots of gui updates). – Alan Oct 15 '12 at 09:42