Using CacheMode From Worker Thread - c#

I'm working with a WPF Canvas that contains a lot of elements. It needs to be able to pan and zoom. WPF cannot handle the layout of the canvas since it has too many elements (I don't know how many, but the XAML file is above 20mb).
The canvas is zoomed and panned using scale and translate transforms on the canvas' rendertransform.
A good trick I found is to use CacheMode. This fixes all panning problems as I generate a cache for the zoom level and set that. Easy, like this:
canvas.CacheMode = new BitmapCache(scale);
The problem is that I need to do it every time the scale changes (which is when the user zooms).
I understand that it will take some time to render the canvas and that is probably unavoidable. But how do I move that rendering to another thread so that it doesn't block the whole UI?
I understand the concept of Background Workers and Dispatcher and such, but I'm not sure how to handle this when the task that actually takes up all the time is the setting of a property that I can only access in the UI thread.
Is it possible to somehow generate the cache in another thread and then transfer it to the UI somehow?
Any other good ideas on how to lessen impact on the UI?

Related

Dealing with very low animation frame-rates

Here's the scenario:
I've got a series of many views primarily containing listboxes. Listboxes, some with large amounts of data in them, some with very little.
Currently, I've got an attached behaviour that essentially just detects when new content has been loaded into the container for these views (a ContentPresenter) and triggers a simple XAML storyboard that offsets the container by 200px to the left, or to the right, and also lowers the opacity to 0% (at 0ms) then over 100ms it restores the opacity to 100% and reduces the offset back to 0px, creating a 'sliding into view' sort of effect.
Now, the issue:
The framerate of this animation varies horribly anywhere from a smooth transition to jittery, to outright just lagging at 1 frame until completion.
This is almost certainly due to the fact it's having to load the new view, render everything in it, then even further; lists with loads of data in them need even more render time, THEN whilst all that's happening it's trying to animate its location changing which seems to drastically affect the performance of the animation.
it shocks me that something as simple as moving some pixels across the screen is so graphically intensive for WPF to handle
Here's a crude representation of the animation itself:
My question:
Is there any sort of fundamental best-practice for dealing with intensive animations in WPF so as to improve their performance, or any kinds of small changes to things that when added up boost performance?
Or is WPF just terrible at animating and we just have to deal with it.
I think your problem is, that you try to do everything at the same time or same thread. Just try to do it async in splitted tasks. There are plenty of different approaches to that. Another approach would be to just handle the work with a semaphore, this should push your performance a bit up.
I've come up with a solution which, in my case, has always solved the jitter problem. It is certainly worth trying. The strategy is to take any element that will not change throughout the animation, insert it in a local reference framework such as a grid or canvas, make that grid or canvas store cached versions of its content, and then apply your animation to the grid or canvas, rather than the inside elements. It is as simple as this:
<Canvas ...Your canvas properties...>
<Canvas.CacheMode>
<BitmapCache />
</Canvas.CacheMode>
...Your UI elements...
</Canvas>
or, for a grid:
<Grid ...Your grid properties...>
<Grid.CacheMode>
<BitmapCache />
</Grid.CacheMode>
...Your UI elements...
</Grid>
You'll want to simply ensure that you do not update anything inside the canvas (or grid), as this will undo the cached version and you'll find yourself with a similar issue where WPF will regenerate its rendering on every frame.
By having cached versions of the content, what WPF will be moving across the screen is not a dynamically updated element, it is simply a bitmap of the last render of that element, and WPF will update that bitmap only if changes are applied to the element.
If that does not solve your issues, it means that performance is hindered by something occurring outside the generation of the local element. This means you'll want to look at your overall application, or reduce the entire frame rate of your application. However, in virtually all cases of jittering and jerky movements I found for my WPF animations, the BitmapCache solution seems to work all the time. Just be careful in not applying animations to objects inside the element being cached.

C# redraw only foreground

I'm working on a college project (simulation) that needs to draw a lot of lines and objects to a custom UserControl. My current approach is just to redraw everything for every tick / update loop using Invalidate(). But it sure needs a lot of time to draw (which is resulting in very low FPS) especially when I need to draw thousands of lines and rectangles.
So how can I only re-draw a specific (group of) objects that move in every update loop and only re-draw the rest (the rarely updated objects) when needed? Or is there other way to optimize the way I draw in this problem?
My idea would be the following:
Draw your Control onto a Bitmap (or other image format) once, and save that Image. Then display that Bitmap on your Control. (Assuming you use a Control that can do that, i.e an ImageBox, not sure what would be best for that).
Whenever you update your graphic, keep track of what sections of your Control may be outdated, then redraw only these sections (drawing only the objects that touch that section), and copy the other sections from your saved image.
Display (and save) the new Image.

How not to block the UI while instantiating a lot of controls

There are lots of articles about how to keep the UI thread free in WPF. Mostly, using async-await to perform long operations in the background. But what can I do when the UI generation itself is heavy?
Say I have a view model that holds a list of items in a property called "Items" of type List, and a view that presents them using an ItemsControl that is bound to that list and generates ui for each item using a data template. I am reading information from the server and while I am getting the data, there is some sort of animated "Busy" indicator that is bound to a IsBusy property in the view model. All is well with that approach and it keeps the UI responsive.
Once the data had returned and the task is done, I am also generating the observable objects (ItemType) for each item in a background task (since no UI is bound to them yet, this is not a problem), and copy the information from the result that came from the server. Still - all nice and responsive.
The last step must happen in the UI thread. Once the List is ready, placing it inside the Items property yields a notification that causes the ItemsControl to start generating UI for each item, and this happens (and must happen) on the UI thread. Since this is a heavy task (there are not a LOT of items but each items has a pretty heavy UI template), the UI hangs and the busy indicator no longer animates.
My question is this: is there a way to keep the UI responsive while the items control is populating? This currently takes between 0.5 - 3 seconds and its too long a task for the UI to remain stuck.
EDIT:
Just to make it clear - this is not a question about Virtualization. I have no problem with the view taking long to create, I just want to find a way to keep to UI responsive while it is being created. I thought about perhaps drawing it while its hidden, or even collapses, but it does no change anything since the UI thread is still busy while doing that, and the busy animation still gets stuck, and the entire UI along with it.
Thanks
Yes. Don't draw all the items at once - only draw those you need to draw immediately. This is otherwise known as UI virtualisation.
Microsoft give a good example of how to implement virtualisation using a canvas at the following link: Virtualized WPF Canvas
Update after comment:
If you hide the panel, the items are actually drawn on to the panel but are invisible. To stop the items appearing one by one, set the panel to Visibility.Hidden and then to Visibility.Visible once you've drawn the items. The panel appears blank and then is suddenly populated.

Animating controls in C#

I'm trying to slide a control to the right when I move the cursor on that control and slide back when the cursor is out.
Since the form contains many control with the same animation, I've decided to let those control slide on different threads. The problem is, when I create a thread and use Control.Invoke() to change the location of the control, the main UI freezes until the animation is completed.
What am I doing wrong? Or is there any way to work around?
Edit: Also used Control.BeginInvoke(). The result is the same.
That happens when you try to move the control faster than UI thread can handle. The below library is a good example of how to do animations with a SINGLE global FRAME-LIMITED timer for all animations.
WinForm Animation Library [.Net3.5+]
A simple library for animating controls/values in .Net WinForm (.Net
3.5 and later). Key frame (Path) based and fully customizable.
https://falahati.github.io/WinFormAnimation/
new Animator2D(
new Path2D(c_control.Location.X, c_control.Location.Y, c_control.Location.X + 100, c_control.Location.Y, 250))
.Play(c_control, Animator2D.KnownProperties.Location);
This moves the c_control control 100 pixels to the right in 250ms.

c# Winforms Controls rendering the components they are positioned over to a bmp (intelligent screenshot?)

The problem:
I have a UserControl (LightBox) which overlays a number of other controls to allow the user to see the controls it is positioned over, but not allow the users to interact with the controls under the Lightbox. Winforms is (almost notoriously as I'm finding out) bad at handling alpha-blending.
Question:
How can I get the Lighbox control to render a bmp (snapshot) of whatever it is positioned over when it is created?
Explanation:
The reason I am looking to do this is to allow the Lightbox to draw its own background from this rendered snapshot. This would (hopefully) stop the issues that I am experiencing with large quantities of flicker and undesirable rendering affects.
Is this even possible?!
Thanks for your help.
EDIT 1:
This question seems to expain some of the issues that I am facing and at least describes what I am attempting to do i.e. create a buffer of the background image. However, there are no links explaining how this might ab achieved
You can use the Control.DrawToBitmap function to draw the control to a Bitmap:

Categories