1

I have a WinForms application, and wanted to add some nice WPF controls, one of them being an 'indeterminate' progress bar.. which just animates nicely until I tell it to stop.

I have done this, and was racking my brains as to why it wasn't animating (changing the value of the progress bar).

I eventually tried showing my form (containing the ElementHost) modally, and hey presto, it animates, but I want to show the form non-modally, as I want to continue processing behind the scenes.

Is there some kind of setting that tells the ElementHost window to continue 'animating'?

Thanks Rich.

Rich S
  • 3,248
  • 3
  • 28
  • 49
  • I have done something similar in the past (WPF Progressbar via ElementHost in a WinForms App) and haven't had the issue you are describing. Have you tried to isolate and reproduce that behavior in a new project? – SvenG Dec 12 '11 at 16:11
  • nope, but I can.. it's quite simple. – Rich S Dec 12 '11 at 16:16
  • ok, I've done this in a separate project, and first of all it worked, but then as soon as I tried to do some processing and a thread.sleep, it stopped animating. Maybe I need to do my database updates in a separate thread.. which will release this thread to continue animating ? or is there a way of telling WPF to do it's stuff on another thread ? – Rich S Dec 12 '11 at 16:30
  • Okay so your database operation is blocking the WPF UI Thread. Try a DispatcherTimer instead of a normal Timer. If that doesn't help have a look at the WPF Dispatcher to update the progressbar. BTW: Avoid Thread.Sleep it isn't good way of Thread programming and should be avoided most times – SvenG Dec 12 '11 at 16:40
  • Don't worry, I don't generally use it, I just wanted to see if it would still animate while the 'current' thread is busy. And also.. I'm not using a 'Timer' I'm using a 'DoubleAnimation' – Rich S Dec 12 '11 at 16:50
  • You could run you database updates using backgroundworked. It is a way of running it on a background thread but it takes care of a lot of the details for you. – paparazzo Dec 12 '11 at 22:37
  • It's cool - once I spotted that the animations run on the GUI thread, I implemented an Asynchronous model on the worker 'process'. Thanks for your suggestions. – Rich S Dec 13 '11 at 09:16
  • PS. what do I do with this question now ? shall I answer it myself ? (I'm relatively new to this !) – Rich S Dec 13 '11 at 09:17

2 Answers2

0

There are many ways to do this , the simplest one is to use a backgroundworker for the lengthy task. The Backgroundworker has an event to report progress. Handle this event and in the handler change your progressbar's value. Just having an animation in the main thread while still doing work on the main thread will not work right ...
One other aproach you may try (though is not what I would recommend for a healty app) is to implement a DoEvents function and call it in the main thread when you want the progress bar to get updated ... Here is the link for the DoEvents implementation:
MSDN DoEvents sugestion
I would Strongly recommend the first approach though

Rayden
  • 135
  • 11
  • Hi, I'm not sure if you read my last two comments against my initial question ? I have resolved this by implementing an Asynchronous method call. I know about the different ways of reporting this information back to the progress bar, my issue was that I didn't realise that the animation ran on the Main GUI thread.. once I knew that, I was able to implement my solution. – Rich S Dec 13 '11 at 14:02
0

As I mention in my comment above, the solution is to run the processing in a separte thread, which allows the .net Main GUI thread do its stuff, and animate the progress bar.

Rich S
  • 3,248
  • 3
  • 28
  • 49