Do xaml animations block the main thread? - wpf

Do the xaml animations block the main thread? I'm showing a big grid with lots of data and several animations and even with everything (but the property changes) on background threads the UI is far from being smooth.
As I have several Dispatcher.BeginInvoke (and some Invoke when needed) I was thinking that maybe the animations make the invokes to go slower. Am I right?
Thanks in advance.

I'd recommend investing in a profiler. Certainly, animations run on the UI thread and heavy use of animations might be the cause of the choppiness, but there is no way for us to know for sure.
My preference for profiling C# is dotTrace, but there are tons of others out there. There are even WPF-specific diagnostic tools from Microsoft in the WPF Performance Suite, but I haven't tried them.

Related

How to keep a winform responsive while a heavy painting is underway

My program draws heavily on a winform. During the drawing, the winform is not responding. How to make it responding to my mouse? I want to use another thread to draw to the winform, but I am afraid I am going to meet the infamous cross-threads-access-conrtrol error.
Here is a great MSDN article that might help you: Give Your .NET-based Application a Fast and Responsive UI with Multiple Thread
When I was facing similar problems, it helped me a great deal to understand what to do.
Also, you might want to have a look at Parallel Programming in .NET Framework 4. This series also includes a strategy with calculations that are spread over multiple synchronized threads, all with using out-of-the-box .NET collections. This is not as complicatesd as it may sound. Just give it a try. :-)
Also, if you have the chance to use the upcoming .NET enhancements, it would be worth to try Asynchronous Programming with Async and Await
If the problem is GDI+ painting you can do all of your drawing on a separate bitmap in a separate thread (instead of drawing directly to screen) - and when you're done: copy the whole bitmap onto the form.
You can use BackgroundWorker class to keep your winform responsive. MSDN.
The problem is when you are performing UI stuff. You have to come back into the UI thread to do so. All I could recommend, is drawing a bit, letting the UI thread process any events, draw again a bit.
Thats probably the best way round, but it isnt easy to do ... Far less easier than just running non UI jobs in another thread ...

What type of application starts faster: Windows Forms or WPF?

As far as I know WPF applications should work faster than Windows Forms applications because WPF applications use DirectX for rendering instead of GDI. But what kind of application will start faster: WPF or Windows Forms? Will the start time be roughly the same or will one type of application be considerably faster than the other?
I am not targeting any specific operating system.
Just to point out, in my experience the WPF renderer is in fact quite a bit slower than Windows Forms, despite using DirectX. This is because lots of tesselation and layout calculations are done on the CPU side, which effectively cancels out the performance gain of using the GPU.
Just google around for "WPF Slow" or "WPF Performance" and you'll find horrified developers shocked to learn their time and effort spent converting WinForms to WPF because of spin about GPUs has been wasted, as the result is slower than the original application.
However ... I will say this. WPF is far superior to WindowsForms in terms of styling, graphical capabilities, developer productivity (databinding), animation, look and feel etc. If performance really is an issue, on certain areas you can optimise. For instance, did you know that you can get an API to write directly to bitmap in WPF (see the WriteableBitmap class) to do GDI-like operations in memory? This is (surprisingly) about 20x faster than the WPF primitives for some operations.
Similarly you can use virtualization to improve the performance of datagrids etc. Telerik have an example of their grid scrolling 1,000,000 rows at interactive framerates.
In conclusion, have a think about what you are doing and see similar examples on the web. Are you doing real-time scientific dataacquisition and visualization or just a standard line of business app? Comparing similar examples to see what the framework is capable of will help you in your decision.
Update 2013 Q3
As an update, in various projects I'm now seeing faster and faster performance from WPF by moving anything and everything off the UI thread. E.g. leave the UI thread just for UI, do as much work as possible using Task Parallel Library or RX. Make everything async and don't block the UI thread while waiting.
Eliminate binding errors as these contribute to WPF performance problems.
You can move animations (e.g. wait or loading animations) onto a different thread for glitch-free animation. You can use render-to-bitmap when doing animated transitions.
The long story short? It is possible to get great performance from WPF applications (as well as slick UI) but you need to put in a bit more work!
I think the start time will be about the same.. I have created a few WPF applications recently and in some cases I think the initial loadtime is a bit slower than win forms, but it is not much and WPF more than makes up for it when the application is up and running
From my experience, I think WPF is quite reactive at start compared to winforms. However, the question is tough, especially considering that WPF can contain the other and vice versa.
I'd follow the philosophy of this article : http://joshsmithonwpf.wordpress.com/2007/09/05/wpf-vs-windows-forms/
WPF and Winforms are just not the same tool, and neither of them is supposed to substitute for the other ;)
The question has a lot of duplicates on this forum, please check here for a link and discussion and all of them : WPF vs. Windows Forms

Can you make animations multithreaded? How to improve animation performance

I'm using the FluidKit Cube Animation in my WPF application.
The animation is smooth when the application window is not in full-screen, but when the application is running in full screen (maximised - borderless window with no controls), the animation is not smooth.
I noticed the WPF application is hammering a single thread and only utilising one of the four cores of the CPU, memory utilisatio is low.
I'm wondering if there is any way of making the window multithreaded or just the animations multithreaded so the application can utilise more of the available hardware?
Are there any other ways to improve animations?
Ben
I will be really excited if someone tells me a UI thread in WPF can leverage multi core CPUs....
Jokes apart, GPU and CPU are different concepts sadly. Will it be controversial if I say "The hardware acceleration mode for WPF apps utilizes GPU's own hyper threading model instead of multi core CPUs."?
Although there are ways to improve animations such as animating Adorners using PresentationSource, Transforming Drawings and Geometries than Shapes, CompositionTarget.Rendering event, BeginStoryBoard.HandoffBehavior="Compose" etc. You will find all of these discussed in details over the internet.
Interestingly there is an old (very useful post) about multi core systems and their impact on parallel .Net frameworks such as PLINQ and WPF 3.0 etc. http://social.msdn.microsoft.com/forums/en-US/wpf/thread/07562a10-17bd-44d3-975b-99ed02bd05de/
I hope this kicks off some very interesting brain storming from our very able StackOverflow experts....
Expectantly Waiting ... :-)
Try to avoid any effects and see how it works and totally avoid bitmap effects, because they force WPF to use software rendering which is not really good.
Suggest you to check out Performance Profiling Tools from Windows SDK. Good luck ;)

Silverlight performance issue!

I have a serious Performance issue in my Silverlight project, when I run my project the CPU usage is going up 80% - 90%
the computer go down!!!, these are the major feature of the project:
1- We are using Silverlight 4.
3- We are using MVVM pattern, which means all data source, states are Binding with the properties in the View Model.
4- Most of the controls are created dynamicly in code.
5- We have a reference for the Main control in the Main view model which is the container of all generated control.
6- In our controls we have many of layouts (Grids).
7- We have Main view model which is the container of all children view models in the system.
8- We have lots of states and animations.
9- We don't use the Relay commands, we are using the regular commands and events, so we raise the event and handle it's parent (we have lots of events).
Is there any of the above causes the performance problem??? I thinks that there is a issue in the Silverlight itself, is there any Silvelight 4 update for performance.
I appreciate any help,
Thanks
A complex application is likely to have performance issues to resolve. The use of data-binding and various events, while great for structuring your application, can make it hard to see what's going on. The control flow may not be obvious, for example code may be executing multiple times due to cascading events/property changes.
I'd recommend using a profiler to see what is consuming most CPU time - JetBrains DotTrace is one I've used with Silverlight, see other questions for more. Then you can focus on the problem areas.
If your application is becoming progressively slower over time, it would be worth investigating whether there are memory leaks also.
Are you using DropShadowEffect anywhere? In which case, you might want to try removing them. I've had this issue once. A template included a DropShadowEffect which made the CPU max out, removing it solved the problem.
In my experience, your 9th point is usually the culprit.
Make sure that you release the event handlers in your classes when you're done with them. In almost all cases where I've seen performance degradation in Silveright or WPF is was the improper handling of event delegates.
As one of the comments pointed out, use a performance profiler and you'll most likely see that the event handlers are consuming an inordinate amount of RAM...meaning that they're sticking around after they're supposed to, and responding to events.
So the performance issue stems from having many objects or controls staying in memory and responding to events.
That's my best guess.
Hope it helps.

How to speed up WPF programs?

I love programming with and for Windows Presentation Framework. Mostly I write browser-like apps using WPF and XAML.
But what really annoys me is the slowness of WPF. A simple page with only a few controls loads fast enough, but as soon as a page is a teeny weeny bit more complex, like containing a lot of data entry fields, one or two tab controls, and stuff, it gets painful.
Loading of such a page can take more than one second. Seconds, indeed, especially on not so fast computers (read: the customers computers) it can take ages.
Same with changing values on the page. Everything about the WPF UI is somehow sluggy.
This is so mean! They give me this beautiful framework, but make it so excruciatingly slow so I'll have to apologize to our customers all the time!
My Question:
How do you speed up WPF?
How do you profile bottlenecks?
How do you deal with the slowness?
Since this seems to be an universal problem with WPF, I'm looking for general advice, useful for many situations and problems.
Some other related questions:
What tools do you use for WPF development
Tools to develop WPF or Silverlight applications
How do you speed up WPF?
Often after using one of the following profiling tools it is obvious what is causing my bottlenecks.
If memory is the issue then I virtualize my data.
If render time is the issue then I virtualize the controls or simplify control templates where possible.
If processing time is the issue I try to improve my algorithm or move that work to a background thread and show a throbber in my ui while the work is going.
How do you profile bottlenecks?
.NET Memory Pofiler
dotTrace
Performance Profiling Tools for WPF
Snoop
Crack.NET
How do you deal with the slowness?
Profiling and counseling.
Install SP1... Loads of very cool performance increases for WPF!!!
Read more here
Here is a example of 2 enhanchements made in SP1: Deffered scrolling & UI Element recyceling!!!
I can not add comments, that's why I post a new answer to this: I've found this video from the pdc09 that gives some ideas about how to profile wpf apps and because it helped me lot, I want to share the link:
Advanced WPF Application Performance Tuning and Analysis
WPF is meant for computers with modern graphics cards. Do your clients have modern graphics cards capable of running Aero? If your clients have older graphics cards, WPF will fall back to software rendering which runs extremely slow in comparison to hardware accelerated graphics.
You also might want to profile your application to make sure that it is actually WPF that is the slow part. It's possible that there is something else that is actually the bottleneck.
avoiding animations also helps a lot sometimes. if you have to use animations, decrease the framerate, this will improve "Feeled" performance
Remove alpha transparency/ bitmap effects.
can you give more details?
I only noticed a slow performance when I use something like a listview or a grid that has some complexity. The solution is to simplify it.
Other than that I only noticed a slow performance when loading the app for the first time.
HTH
I find it helpful to side-step the XAML, and write the entire UI in C#. This lets me precisely control when the controls are created and loaded. It also helps me understand what XAML is doing "under the covers".

Resources