Destroying controls in WPF

invidia

Platinum Member
Oct 8, 2006
2,151
1
0
My app is continuously making labels. But after a certain period of time, I don't need them anymore. Is there a way to destroy a label? I've tried dispose() but it seems that WPF controls don't have the dispose() method.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Remove them from their host, and set to null (or nothing)... Collect Garbage?

Why are you continously making labels? Why not just reuse the ones you have?
 

invidia

Platinum Member
Oct 8, 2006
2,151
1
0
It's the way my app works. There is probably a way better method though, if anyone can help.

What it does is something similar to WoW's scrolling combat text. Everytime the user does a specific action, I have the program make a label, apply whatever style, and then apply the animation for fading. After it finishes fading, I dont need it anymore. I can't pre-define a number of labels because the rate of the user's actions are unpredictable.

It's the best solution I've come with based on my inexperience with WPF.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
I sort of miss this in C#, it doesn't really give the ability to really suggest any sort of memory control. This was by design, but still, it is nice to be able to say "Yes, I will no longer use this piece of memory, you can free it up."

Disposed is only for the situation where C# interacts with some unmanaged language (EG C++). Since WPF is completely managed, there is no memory control on WPF objects.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I sort of miss this in C#, it doesn't really give the ability to really suggest any sort of memory control. This was by design, but still, it is nice to be able to say "Yes, I will no longer use this piece of memory, you can free it up."

Disposed is only for the situation where C# interacts with some unmanaged language (EG C++). Since WPF is completely managed, there is no memory control on WPF objects.

Nah, I disagree. You don't need to worry about it if you follow the correct paradigms, and the runtime is far better at it than most developers anyway. The .NET runtime GC strategy is specifically optimized for the OP's case (i.e. lots of small objects with very short lifespans).
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
invidia, I believe with what you have setup, the only solution is to do what you are doing.

Also Mark. The problem with GC though is that it only cleans up when the CPU is idle. If you try to make a game which has an infinite framerate, the GC will never run. I had this problem attempting to use SlimDX (a managed wrapper around DirectX.) It would create a background color object every frame when you cleared the scene, but since the game is constantly rendering, it would never clean up the background color object even though it only had a limited lifespan (for the call to clear the screen)... After running at 3000 fps (in my sample, which rendered nothing) for 3 minutes, I had a gig of memory used. I had to stop using SlimDX and go back to C++ as a development platform for games. To have the game stop periodically for a few seconds to allow GC to run would not be an acceptable solution. Also running GC.Collect, GC.WaitForPendingFinalizers took too much framerate every frame.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I understand there are corner cases where you may need to take more control, and there are also just inherent limitations of a managed environment. But keeping the 80/20 rule in mind, I stand by my statement.
 

invidia

Platinum Member
Oct 8, 2006
2,151
1
0
I have it set up by setting the control to null and forcing a GC.collect() after the animation finish playing. After running it for hours the memory it was using looked constant, so I think that worked.
 

Oyster

Member
Nov 20, 2008
151
0
0
The problem with GC though is that it only cleans up when the CPU is idle.

That is not a 100&#37; accurate. The GC cleans up when there is more memory needed while you're "newing" objects and when the managed heap is full. Also, the CLR's GC is optimized to the point that the objects are managed in "generations." I believe there are three generations (this might have changed between .NET 2.0 and 4.0) - specifically, GC gets invoked when generation zero is full. In abstract terms, when an object isn't referenced anymore, it gets put on generation two, then gets promoted to generation one, and then to zero. There are many other enhancements which happen while traversing generations (to account for circular references and "redundant walks") and when promoting objects from one generation to the other...

As Mark pointed out earlier, the current generation of runtime heaps are optimized so much so that they would give their sister C-runtime heaps a serious run for their monies.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |